Java Operators
Operators in Java are special symbols that perform operations on variables and values. They form the building blocks of expressions and logic in Java programs. For example, the + operator adds two numbers, while the == operator compares two values.
Understanding Java operators is essential because they are used everywhere in programming — from arithmetic calculations to decision-making and logic building.
Types of Java Operators
Java provides a rich set of operators, which can be grouped into several categories:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Ternary (Conditional) Operator
- Type Comparison Operator (instanceof)
Let’s explore each in detail.
1. Arithmetic Operators
These operators are used to perform basic mathematical operations.
Operator | Description | Example (a=10, b=5 ) | Result |
---|---|---|---|
+ | Addition | a + b | 15 |
- | Subtraction | a - b | 5 |
* | Multiplication | a * b | 50 |
/ | Division | a / b | 2 |
% | Modulus (Remainder) | a % b | 0 |
Example:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(“a + b = ” + (a + b));
System.out.println(“a – b = ” + (a – b));
System.out.println(“a * b = ” + (a * b));
System.out.println(“a / b = ” + (a / b));
System.out.println(“a % b = ” + (a % b));
}
}
2. Relational (Comparison) Operators
These operators compare two values and return a boolean result (true
or false
).
Operator | Description | Example (a=10, b=20 ) | Result |
---|---|---|---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | false |
< | Less than | a < b | true |
>= | Greater than or equal to | a >= b | false |
<= | Less than or equal to | a <= b | true |
Example:
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(“a == b : ” + (a == b));
System.out.println(“a != b : ” + (a != b));
System.out.println(“a > b : ” + (a > b));
System.out.println(“a < b : ” + (a < b));
}
}
3. Logical Operators
These operators are used to combine multiple conditions.
Operator | Description | Example (a=10, b=20 ) | Result |
---|---|---|---|
&& | Logical AND | (a < 15 && b > 15) | true |
` | ` | Logical OR | |
! | Logical NOT | !(a < b) | false |
Example:
public class LogicalExample {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println((a < 15 && b > 15)); // true
System.out.println((a > 15 || b > 15)); // true
System.out.println(!(a < b)); // false
}
}
4. Assignment Operators
These operators assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= | a = 5 | a = 5 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
Example:
public class AssignmentExample {
public static void main(String[] args) {
int a = 10;
a += 5; // a = a + 5
System.out.println(“a = ” + a);
}
}
5. Unary Operators
These operators work with a single operand.
Operator | Description | Example (a=5 ) | Result |
---|---|---|---|
+ | Unary plus | +a | 5 |
- | Unary minus | -a | -5 |
++ | Increment | a++ or ++a | 6 |
-- | Decrement | a-- or --a | 4 |
! | Logical NOT | !(a < 10) | false |
Example:
public class UnaryExample {
public static void main(String[] args) {
int a = 5;
System.out.println(“a++ = ” + (a++)); // Post-increment
System.out.println(“++a = ” + (++a)); // Pre-increment
}
}
6. Bitwise Operators
These operators perform operations at the bit level.
Operator | Description | Example (a=5, b=3 ) | Binary Operation | Result |
---|---|---|---|---|
& | Bitwise AND | a & b | 0101 & 0011 = 0001 | 1 |
` | ` | Bitwise OR | `a | b` |
^ | Bitwise XOR | a ^ b | 0101 ^ 0011 = 0110 | 6 |
~ | Bitwise NOT | ~a | ~0101 = 1010 (2’s comp) | -6 |
<< | Left Shift | a << 1 | 0101 << 1 = 1010 | 10 |
>> | Right Shift | a >> 1 | 0101 >> 1 = 0010 | 2 |
>>> | Unsigned Shift | a >>> 1 | Similar to >> but fills with 0 | 2 |
Example:
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println(“a & b = ” + (a & b));
System.out.println(“a | b = ” + (a | b));
System.out.println(“a ^ b = ” + (a ^ b));
System.out.println(“a << 1 = ” + (a << 1));
}
}
7. Ternary (Conditional) Operator
The ternary operator is a shorthand for if-else conditions.
Syntax:
condition ? expression1 : expression2
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 is: ” + max);
}
}
8. Type Comparison Operator (instanceof)
The instanceof operator is used to test whether an object is an instance of a specific class or subclass.
Example:
class Animal {}
class Dog extends Animal {}
public class InstanceofExample {
public static void main(String[] args) {
Animal obj = new Dog();
System.out.println(obj instanceof Dog); // true
System.out.println(obj instanceof Animal); // true
}
}

Conclusion
Operators in Java provide the ability to perform calculations, comparisons, logic building, and decision-making. By mastering arithmetic, relational, logical, assignment, unary, bitwise, ternary, and type comparison operators, you can write powerful and efficient Java programs.