Bca IV Sem Unit I (Java)
Bca IV Sem Unit I (Java)
BCA IV SEM
Subject: Java Programming
Unit I:
Introduction to Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995 and later
acquired by Oracle Corporation. It is a simple programming language. Java makes writing,
compiling, and debugging programming easy. It helps to create reusable code and modular
programs. Java is a class-based, object-oriented programming language and is designed to have as
few implementation dependencies as possible. A general-purpose programming language made for
developers to write once run anywhere that is compiled Java code can run on all platforms that
support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine.
The syntax of Java is similar to c/c++.
History: Java’s history is very interesting. It is a programming language created in 1991. James
Gosling, Mike Sheridan, and Patrick Naughton, a team of Sun engineers known as the Green team
initiated the Java language in 1991. Sun Microsystems released its first public implementation in
1996 as Java 1.0. It provides no-cost -run-times on popular platforms. Java1.0 compiler was re-
written in Java by Arthur Van Hoff to strictly comply with its specifications. With the arrival of
Java 2, new versions had multiple configurations built for different types of platforms.
1|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming languages in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has huge community support (tens of millions of developers)
• Java is an object-oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice
versa.
Initially, the name oak was given to the language. However, the team decided that a new name
should be given to the language, and words like, DNA, revolutionary, Jolt, Dynamic, Silk, etc. were
suggested. All these names were fun to say and easy to spell out. But what was missing was the
essence of the language in the suggested names, which the team wanted to have. As per James
Gosling, Java, and Silk were two of the most popular options, and since Java had a unique name,
most people preferred it.
Java is also a name of an island in Indonesia where coffee (named Java Coffee) was produced. The
name Java was chosen by James Gosling because he was having coffee near his office. Readers
should note that Java is not an acronym. It is just a name.
2|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
1. Platform Independent: Compiler converts source code to bytecode and then the JVM executes
the bytecode generated by the compiler. This bytecode can run on any platform be it Windows,
Linux, or macOS which means if we compile a program on Windows, then we can run it on Linux
and vice versa. Each operating system has a different JVM, but the output produced by all the OS is
the same after the execution of the bytecode. That is why we call java a platform-independent
language.
❖ Abstraction
❖ Encapsulation
❖ Inheritance
❖ Polymorphism
3. Simple: Java is one of the simple languages as it does not have complex features like pointers,
operator overloading, multiple inheritances, and Explicit memory allocation.
4. Robust: Java language is robust which means reliable. It is developed in such a way that it puts
a lot of effort into checking errors as early as possible, that is why the java compiler is able to detect
even those errors that are not easy to detect by another programming language. The main features of
java that make it robust are garbage collection, Exception Handling, and memory allocation.
5. Secure: In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it shows
ArrayIndexOutOfBound Exception if we try to do so. That’s why several security flaws like stack
corruption or buffer overflow are impossible to exploit in Java. Also, java programs run in an
environment that is independent of the os(operating system) environment which makes java
programs more secure.
6. Distributed: We can create distributed applications using the java programming language.
Remote Method Invocation and Enterprise Java Beans are used for creating distributed applications
in java. The java programs can be easily distributed on one or more systems that are connected to
each other through an internet connection.
3|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
8. Portable: As we know, java code written on one machine can be run on another machine. The
platform-independent feature of java in which its platform-independent bytecode can be taken to
any platform for execution makes java portable.
9. High Performance: Java architecture is defined in such a way that it reduces overhead during
the runtime and at some times java uses Just In Time (JIT) compiler where the compiler compiles
code on-demand basics where it only compiles those methods that are called making applications to
execute faster.
10. Dynamic flexibility: Java being completely object-oriented gives us the flexibility to add
classes, new methods to existing classes, and even create new classes through sub-classes. Java
even supports functions written in other languages such as C, C++ which are referred to as native
methods.
11. Sandbox Execution: Java programs run in a separate space that allows user to execute their
applications without affecting the underlying system with help of a bytecode verifier. Bytecode
verifier also provides additional security as its role is to check the code for any violation of access.
12. Write Once Run Anywhere: As discussed above java application generates a ‘.class’ file that
corresponds to our applications(program) but contains code in binary format. It provides ease t
architecture-neutral ease as bytecode is not dependent on any machine architecture. It is the primary
reason java is used in the enterprising IT industry globally worldwide.
13. Power of compilation and interpretation: Most languages are designed with the purpose of
either they are compiled language or they are interpreted language. But java integrates arising
enormous power as Java compiler compiles the source code to bytecode and JVM executes this
bytecode to machine OS-dependent executable code.
4|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
JAVA Terminology
Before moving further, there are some Java terms that we need to understand. So let's have a brief
understanding of the Java terminology.
Here, as we use JDK, we need an environment to run the programs. We use JRE Java Runtime
Environment, which provides the least requirements to execute the Java program. It provides the
JVM, Core classes, and supporting files.
5|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
When our software tends to execute a particular program, it requires some environment to run in.
Usually, it's any operating system, for example, Unix, Linux, Microsoft Windows, or macOS. Here
JRE acts as a translator and facilitator between the java program and the operating system.
Compile the Code: The Java Development Kit(JDK) provides us with the JAVAC compiler to get
through this step. We basically require the JDK to convert our source code into a specific format
(compiled code) that can be easily interpreted by the Java Runtime Environment (JRE).
Run the Code: JVM runs the bytecode provided by the compiler. Since Java is a platform-
independent language, the compiled code produced by the JAVAC compiler is converted to
machine code using platform specific JVMs. Different platforms have different JVMs. JVMs
convert the bytecode into platform specific machine code.
6|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Let’s now see how this bytecode is developed, making it easy for us to execute our programs.
Garbage Collector
Within Java, the programmers cannot delete the objects. Hence, to delete or recollect a memory,
JVM has a Garbage Collector. These Garbage Collectors can recollect those objects which have not
been referenced. Using this Java makes the life of a programmer very convenient by managing the
memory. Despite this, programmers should be aware of what they are writing in their codes or if
they are using objects that have already been used for longer. This is because this collector can’t
restore the memory of objects that are referenced. Garbage collection here is an automatic process.
Class Path
Classpath is a particular file path where both the Java runtime and Java compiler look for .class files
to appear. Although by default, the Java Development Kit provides us with quite a few libraries.
Here you can also include an external set of libraries, but they should be added to the classpath
beforehand.
7|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
import java.io.*;
Output:
Welcome to JAVA
Explanation:
1. import java.io.- Import is a keyword that includes the class we use in the code. This is used to
import all the classes of the io package. Java's io package includes a set of input and output streams
used to read or write files and input or output.
2. public class-The data and methods that need to be used in the program are included in the class.
Methods define the behaviour of the class.
3. public static-Static methods do not require objects to get called. They can be executed using the
dot operator with the class name.
5. main()- Name of the method. Entry point method where JVM runs the program.
6. (String[ ] args)- These are used as command-line arguments and are passed as strings.
7. System.out.println- This command prints the contents provided within the print statement.
8|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Points to Remember
9|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
In Java language, primitive data types are the building blocks of data manipulation. These are the
most basic data types available in Java language.
10 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
11 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual
conversion performed by the programmer.
Type casting is when you assign a value of one primitive data type to another type.
Widening Casting:
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example:
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting:
Narrowing casting must be done manually by placing the type in parentheses in front of the value:
Example
public class Main {
12 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Operators in Java
Java provides many types of operators which can be used according to the need. They are classified
based on the functionality they provide. In this article, we will learn about Java Operators and learn
all their types.
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.
13 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a value.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called
x:
Example
int x = 10;
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Example
int x = 5;
int y = 3;
System.out.println(x > y); // returns true, because 5 is higher than 3
6. Ternary operator
15 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
The ternary operator is a shorthand version of the if-else statement. It has three operands and hence
the name Ternary.
7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can be
used with any of the integer types. They are used when performing update and query operations of
the Binary indexed trees.
& Bitwise AND operator: returns bit by bit AND of input values.
| Bitwise OR operator: returns bit by bit OR of input values.
^ Bitwise XOR operator: returns bit-by-bit XOR of input values.
~ Bitwise Complement Operator: This is a unary operator which returns the one’s complement
representation of the input value, i.e., with all bits inverted.
// Driver class
class Operator {
// main function
public static void main(String[] args)
{
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
System.out.println("d & e: " + (d & e));
System.out.println("d | e: " + (d | e));
System.out.println("d ^ e: " + (d ^ e));
System.out.println("~d: " + (~d));
System.out.println("d << 2: " + (d << 2));
System.out.println("e >> 1: " + (e >> 1));
System.out.println("e >>> 1: " + (e >>> 1));
16 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
}
}
8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or dividing
the number by two, respectively. They can be used when we have to multiply or divide a number by
two. General format-
<< Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result.
Similar effect as multiplying the number with some power of two.
>> Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as
a result. The leftmost bit depends on the sign of the initial number. Similar effect to dividing the
number with some power of two.
>>> Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left
as a result. The leftmost bit is set to 0.
int a = 10;
9. instanceof operator
The instance of the operator is used for type checking. It can be used to test if an object is an
instance of a class, a subclass, or an interface. General format-
17 | P a g e
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, Java provides statements that can be used to
control the flow of Java code. Such statements are called control flow statements. It is one of the
fundamental features of Java, which provides a smooth flow of program.
Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-
each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is
given below.
Example:
public class Sample {
public static void main(String[] args) {
String[] names = {"Java","C","C++","Python","JavaScript"};
System.out.println("Printing the content of the array:");
for(String name:names) {
System.out.println(name);
}
18 | P a g e
} Study Notes (For Private Circular Only)
}
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit I:
Output:
Printing the content of the array:
Java
C
C++
Python
JavaScript
The java command-line argument is an argument i.e. passed at the time of running the java
program.
The arguments passed from the console can be received in the java program and it can be used as an
input.
So, it provides a convenient way to check the behavior of the program for the different values. You
can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}
19 | P a g e
Study Notes (For Private Circular Only)