Loading content...
Loading content...
Operators are symbols used to perform operations on variables (operands).
Arithmetic Operators
Assignment Operator
Relational Operators
Logical Operators
Bitwise Operators
Ternary Operator
Increment/Decrement Operator
new Operator
instanceof Operator
Used to perform mathematical operations.
OperatorDescription => + Addition - Subtraction * Multiplication / Division % Modulus
Example
java
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.
Used to assign value from right side to left side variable.
java
variable = value;Type casting is converting one data type into another compatible type.
1. Implicit Type Casting (Widening)
Done automatically by JVM
When destination type is larger than source
java
byte b = 10;
int a = b;2. Explicit Type Casting (Narrowing)
Done manually
May cause data loss
java
int a = 130;
byte b = (byte) a;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
java
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 !=).
Used with boolean expressions.
Returns true only if both conditions are true
java
System.out.println(true && true); // true
System.out.println(true && false); // false
System.out.println(false && true); // false
System.out.println(false && false); // falseReturns true if at least one condition is true
java
System.out.println(true || true); // true
System.out.println(true || false); // true
System.out.println(false || true); // true
System.out.println(false || false); // falseReverses the boolean value
java
System.out.println(!true); // false&& → stops if first condition is false
|| → stops if first condition is true
Operate on binary (bits).
Operate on binary (bits).
OperatorMeaning & -> AND | -> OR ^ -> XOR << -> Left Shift >> -> Right Shift >>> -> Unsigned Right Shift ~ -> Complement
java
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 = 2Short form of if-else.
Syntax
java
condition ? value1 : value2;Example
java
int a = 10;
int b = 20;
String result = (a > b) ? "A is greater" : "B is greater";java
int x = 10;
int y = ++x; // x first increasesjava
int x = 10;
int y = x++; // x increases laterjava
int x = 10;
int y = --x;java
int x = 10;
int y = x--;Used to create objects and allocate memory in heap.
java
Test t1 = new Test();new → allocates memory
Test() → constructor call
t1 → reference variable
Used to check object type.
java
if (obj instanceof String) {
System.out.println("It is a String");