Maharishi University of Information Technology
Course: BCA Subject code: BCA -202
Year/Semester: 1/2nd
Unit: 2
Session: 2025-26
Faculty: Mr. Harshit Singh
Subject Name: OOP’s Using Java
CHAPTER 2: - Operators and Expression in Java
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of
operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Java Operator Precedence
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<=
>>= >>>=
Java Conditions and If Statements
You already know that Java supports the usual logical conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true.
Example:-
if (20 > 18) {
[Link]("20 is greater than 18");
}
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
Example:-
int time = 20;
if (time < 18) {
[Link]("Good day.");
} else {
[Link]("Good evening.");
}
// Outputs "Good evening."
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
int time = 22;
if (time < 10) {
[Link]("Good morning.");
} else if (time < 18) {
[Link]("Good day.");
} else {
[Link]("Good evening.");
}
// Outputs "Good evening."
Java Switch:-
Instead of writing many if..else statements, you can use the switch statement. The switch statement selects one of
many code blocks to be executed:
This is how it works:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
The break and default keywords are optional.
Example:-
int day = 4;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
}
// Outputs "Thursday" (day 4)
The break Keyword
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
The default Keyword
The default keyword specifies some code to run if there is no case match:
Example
int day = 4;
switch (day) {
case 6:
[Link]("Today is Saturday");
break;
case 7:
[Link]("Today is Sunday");
break;
default:
[Link]("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"
Java While Loop
The while loop loops through a block of code as long as a specified condition is true:
Example
int i = 0;
while (i < 5) {
[Link](i);
i++;
}
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition is true.
Example
int i = 0;
do {
[Link](i);
i++;
}
while (i < 5);
Java For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while
loop:
Example: -
for (int i = 0; i < 5; i++) {
[Link](i);
}
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop. The "inner loop" will be executed one
time for each iteration of the "outer loop":
Example: -
public static void main(String[] args) {
// Outer loop.
for (int i = 1; i <= 2; i++) {
[Link]("Outer: " + i); // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; j++) {
[Link](" Inner: " + j); // Executes 6 times (2 * 3)
}
}
}
}
For-Each Loop
There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data sets):
Syntax
for (type variableName : arrayName) {
// code block to be executed
}
Example
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
[Link](i);
}
Java Break and Continue:-
Java Break:-
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a
switch statement. The break statement can also be used to jump out of a loop. This example stops the loop when i is
equal to 4:
Example: -
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
[Link](i);
}
}
}
Java Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next
iteration in the loop. This example skips the value of 4:
Example: -
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
[Link](i);
}
}
}
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String [] cars= {"Volvo", "BMW", "Ford", "Mazda"};
Access the Elements of an Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
[Link](cars [0]);
// Outputs Volvo
Change an Array Element
To change the value of a specific element, refer to the index number:
Example
cars [0] = "Opel";
Array Length
To find out how many elements an array has, use the length property:
Example
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
[Link]([Link]);
// Outputs 4
Loop Through an Array
You can loop through the array elements with the for loop, and use the length property to specify how many times the
loop should run.
The following example outputs all elements in the cars array:
Example
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < [Link]; i++) {
[Link](cars[i]);
}
Loop Through an Array with For-Each
There is also a "for-each" loop, which is used exclusively to loop through elements in arrays:
Syntax
for (type variable : arrayname) {
...
}
The following example outputs all elements in the cars array, using a "for-each" loop:
Example: -
public class Main {
public static void main (String [] args) {
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
[Link](i);
}
}
}
Multidimensional Arrays
A multidimensional array is an array of arrays.
Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns.
To create a two-dimensional array, add each array within its own set of curly braces:
Example: -
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
Access Elements
To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside
that array. This example accesses the third element (2) in the second array (1) of myNumbers:
Example: -
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
[Link](myNumbers[1][2]);
// Outputs 7
Change Element Values
You can also change the value of an element:
Example: -
Int [][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers [1][2] = 9;
[Link](myNumbers [1][2]);
// Outputs 9 instead of 7
Loop Through a Multi-Dimensional Array
You can also use a for loop inside another for loop to get the elements of a two-dimensional array (we still have to point
to the two indexes):
Example: -
public class Main {
public static void main (String [] args) {
int [] [] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < [Link]; ++i) {
for (int j = 0; j < myNumbers[i].length; ++j) {
[Link](myNumbers[i][j]);
}
}
}
}