Lec 3
Lec 3
• 1 - Arithmetic Operators
• 2 - Relational (Comparison) Operators
• 3 - Logical Operators
• 4 - Assignment Operators
• 5 - Unary Operators
• 6 - Bitwise Operators
• 7 - Ternary Operator
• 8 - Instanceof Operator
• 1. Arithmetic Operators
• Used to perform basic mathematical operations.
• Example-
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
• 2. Relational (Comparison) Operators
• Used to compare two values.
• Example-
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Equal to: " + (a == b)); // false
System.out.println("Not equal to: " + (a != b)); // true
System.out.println("Greater than: " + (a > b)); // true
System.out.println("Less than: " + (a < b)); // false
System.out.println("Greater than or equal to: " + (a >= b)); // true
System.out.println("Less than or equal to: " + (a <= b)); // false
}
}
• 3. Logical Operators
• Used for logical operations.
• Example-
public class LogicalExample {
public static void main(String[] args) {
int a = 10, b = 5, c = 15;
// bitwise complement of 35
result = ~number;
System.out.println(result); // prints -36
}
}
• Left Shift Operator
• The left shift operator shifts all bits towards the left by a certain
number of specified bits. It is denoted by <<.
int number = 2;
int number1 = 8;
int number2 = -8;
int number1 = 8;
int number2 = -8;
• Example –
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max); // 20
}
}
• 8 - Instanceof Operator
• Used to check if an object is an instance of a particular class.
• Example –
String str = "Hello";
System.out.println(str instanceof String); // true