java programming notes1
java programming notes1
Overview of Java
Java is a class-based, object-oriented programming language that is designed to have as
few implementation dependencies as possible. It is intended to let application
developers Write Once and Run Anywhere (WORA), meaning that compiled Java code
can run on all platforms that support Java without the need for recompilation. Java was
developed by James Gosling at Sun Microsystems Inc. in May 1995 and later acquired by
Oracle Corporation and is widely used for developing applications for desktop, web, and
mobile devices.
Java applications are compiled to byte code that can run on any Java Virtual Machine. The
syntax of Java is similar to C/C++. Bytecode is an intermediate, platform-independent code
generated after Java source code is compiled. It is not directly executed by the operating
system but runs on the Java Virtual Machine (JVM), which translates it into machine-
specific instructions.
Key Features of Bytecode:
It is stored in .class files after compilation.
It enables Write Once, Run Anywhere (WORA) functionality.
It is interpreted or compiled by the JVM at runtime.
It enhances security and portability across different systems.
Arrays in Java
Arrays are fundamental structures in Java that allow us to store multiple values of the same
type in a single variable. They are useful for storing and managing collections of data.
Single-Dimensional Array in Java
A single-dimensional array in Java is a linear collection of elements of the same data type. It
is declared and instantiated using the following syntax:
dataType[] arr; (or) dataType []arr; (or) dataType arr[];
Instantiation of an Array in Java
arrayRefVar=new datatype[size];
Declaration, Instantiation and Initialization of Java Array
In Java, you can declare, instantiate, and initialize an array in a single line, as demonstrated
below:
int a[]={33,3,4,5};
Example:
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
Output:
33
3
4
5
Example:
class TestMultiArray {
public static void main(String args[]) {
int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // 3x3 matrix
// Printing the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Output:
123
456
789
Java Operators
Java operators are special symbols that perform operations on variables or values.
Types of Operators in Java
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
Arithmetic Operators are used to perform simple arithmetic operations on primitive and non-
primitive data types.
* : Multiplication
/ : Division
% : Modulo
+ : Addition
– : Subtraction
2. Unary Operators
Unary Operators need only one operand. They are used to increment, decrement, or negate a
value.
- , Negates the value.
+ , Indicates a positive value (automatically converts byte, char, or short to int).
++ , Increments by 1.
o Post-Increment: Uses value first, then increments.
o Pre-Increment: Increments first, then uses value.
-- , Decrements by 1.
o Post-Decrement: Uses value first, then decrements.
o Pre-Decrement: Decrements first, then uses value.
! , Inverts a boolean value.
3. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the variable
on the left, and therefore right-hand side value must be declared before using it or should be a
constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with others to create shorthand
compound statements. For example, a += 5 replaces a = a + 5. Common compound operators
include:
+= , Add and assign.
-= , Subtract and assign.
*= , Multiply and assign.
/= , Divide and assign.
%= , Modulo and assign.
4. Relational Operators
Relational Operators are used to check for relations like equality, greater than, and less than.
They return boolean results after the comparison and are extensively used in looping
statements as well as conditional if-else statements. The general format is ,
variable relation_operator value
Relational operators compare values and return boolean results:
== , Equal to.
!= , Not equal to.
< , Less than.
<= , Less than or equal to.
> , Greater than.
>= , Greater than or equal to.
5. Logical Operators
Logical Operators are used to perform “logical AND” and “logical OR” operations, similar to
AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the
second condition is not evaluated if the first is false.
Conditional operators are:
&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operator
The Ternary Operator is a shorthand version of the if-else statement. It has three operands and
hence the name Ternary. The general format is ,
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements
after the ‘?’ else execute the statements after the ‘:’.
7. Bitwise Operators
Bitwise Operators are used to perform the manipulation of individual bits of a number and
with any of the integer types. They are used when performing update and query operations of
the Binary indexed trees.
& (Bitwise AND) – returns bit-by-bit AND of input values.
| (Bitwise OR) – returns bit-by-bit OR of input values.
^ (Bitwise XOR) – returns bit-by-bit XOR of input values.
~ (Bitwise Complement) – inverts all bits (one’s complement).
8. Shift Operators
Shift 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. The general format ,
number shift_op number_of_places_to_shift;
<< (Left shift) – Shifts bits left, filling 0s (multiplies by a power of two).
>> (Signed right shift) – Shifts bits right, filling 0s (divides by a power of two), with
the leftmost bit depending on the sign.
>>> (Unsigned right shift) – Shifts bits right, filling 0s, with the leftmost bit always
0.
9. instanceof operator
The instance of 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. The general format ,
object instance of class/subclass/interface
Example:
Person obj1 = new Person();
System.out.println("obj1 instanceof Person: " + (obj1 instanceof Person));
Output
obj1 instanceof Person: true