Operators
Definition
Operators are symbols used to perform operations on variables (operands).
Types of Operators
Arithmetic Operators
Assignment Operator
Relational Operators
Logical Operators
Bitwise Operators
Ternary Operator
Increment/Decrement Operator
new Operator
instanceof Operator
1. Arithmetic Operators
Used to perform mathematical operations.
OperatorDescription => + Addition - Subtraction * Multiplication / Division % Modulus
Example
int a = 100;
int b = 20;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);Note: Arithmetic operations cannot be performed on boolean values.
2. Assignment Operator (=)
Used to assign value from right side to left side variable.
Syntax
variable = value;Type Casting
Definition
Type casting is converting one data type into another compatible type.
Types of Type Casting
1. Implicit Type Casting (Widening)
Done automatically by JVM
When destination type is larger than source
byte b = 10;
int a = b;2. Explicit Type Casting (Narrowing)
Done manually
May cause data loss
int a = 130;
byte b = (byte) a;3. Relational Operators
Used to compare two values.
Result is always boolean (true/false).
OperatorMeaning > Greater than < Less than >= Greater than equal <= Less than equal ==Equal !=Not equal
Example
int a = 20;
int b = 10;
System.out.println(a > b);
System.out.println(a == b);Note: Relational operators cannot be used with boolean values (except == and !=).
4. Logical Operators
Used with boolean expressions.
1. Logical AND (&&)
Returns true only if both conditions are true
System.out.println(true && true); // true
System.out.println(true && false); // false
System.out.println(false && true); // false
System.out.println(false && false); // false2. Logical OR (||)
Returns true if at least one condition is true
System.out.println(true || true); // true
System.out.println(true || false); // true
System.out.println(false || true); // true
System.out.println(false || false); // false3. Logical NOT (!)
Reverses the boolean value
System.out.println(!true); // falseShort-Circuit Behavior
&&→ stops if first condition is false||→ stops if first condition is true
5. Bitwise Operators
Operate on binary (bits).
Operate on binary (bits).
OperatorMeaning & -> AND | -> OR ^ -> XOR << -> Left Shift >> -> Right Shift >>> -> Unsigned Right Shift ~ -> Complement
Example
int a = 5; // 0101
int b = 7; // 0111
System.out.println(a & b); // 0101 = 5
System.out.println(a | b); // 0111 = 7
System.out.println(a ^ b); // 0010 = 26. Ternary Operator
Short form of if-else.
Syntax
condition ? value1 : value2;Example
int a = 10;
int b = 20;
String result = (a > b) ? "A is greater" : "B is greater";7. Increment / Decrement Operators
Increment (++)
Pre-increment
int x = 10;
int y = ++x; // x first increasesPost-increment
int x = 10;
int y = x++; // x increases laterDecrement (--)
Pre-decrement
int x = 10;
int y = --x;Post-decrement
int x = 10;
int y = x--;8. new Operator
Used to create objects and allocate memory in heap.
Example
Test t1 = new Test();new→ allocates memoryTest()→ constructor callt1→ reference variable
9. instanceof Operator
Used to check object type.
Example
if (obj instanceof String) {
System.out.println("It is a String");
}