Operators in Java – Complete Guide with Examples
Java operators are symbols that perform operations on variables and values. In this blog, we will understand each type of operator in Java with simple examples.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
Example:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulus: " + (a % b));
}
}
2. Relational (Comparison) Operators
These operators compare two values and return a boolean result (true or false).
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example:
public class RelationalExample {
public static void main(String[] args) {
int a = 10;
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);
System.out.println(a <= b);
}
}
3. Logical Operators
Logical operators are used with boolean conditions.
| Operator | Description | ||
|---|---|---|---|
| && | Logical AND | ||
| Logical OR | |||
| ! | Logical NOT |
Example:
public class LogicalExample {
public static void main(String[] args) {
int age = 25;
System.out.println(age > 18 && age < 30); // true
System.out.println(age > 18 || age < 10); // true
System.out.println(!(age > 18)); // false
}
}
4. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
| = | a = 5 | Assign |
| += | 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;
System.out.println(a);
a -= 3;
System.out.println(a);
a *= 2;
System.out.println(a);
a /= 4;
System.out.println(a);
a %= 3;
System.out.println(a);
}
}
5. Increment and Decrement Operators
These operators increase or decrease the value by 1.
| Operator | Description |
|---|---|
| ++ | Increment |
| -- | Decrement |
Example:
public class IncrementExample {
public static void main(String[] args) {
int a = 5;
System.out.println(a++); // Post-increment
System.out.println(++a); // Pre-increment
System.out.println(a--); // Post-decrement
System.out.println(--a); // Pre-decrement
}
}
Difference:
a++ → First use, then increase
++a → First increase, then use
6. Ternary Operator
The ternary operator is a shortcut for if-else.
Syntax:
condition ? value_if_true : value_if_false;
Example:
public class TernaryExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);
}
}
7. Bitwise Operators
Bitwise operators work on bits (binary values).
| Operator | Description | |
|---|---|---|
| & | AND | |
| OR | ||
| ^ | XOR | |
| ~ | NOT | |
| << | Left Shift | |
| >> | Right Shift |
Example:
public class BitwiseExample {
public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);
System.out.println(a << 1);
System.out.println(a >> 1);
}
}
Final Summary
| Operator Type | Operators | ||
|---|---|---|---|
| Arithmetic | + - * / % | ||
| Relational | == != > < >= <= | ||
| Logical | && | ! | |
| Assignment | = += -= *= /= %= | ||
| Increment/Decrement | ++ -- | ||
| Ternary | ? : | ||
| Bitwise | & | ^ ~ << >> |
Conclusion
Operators are the basic building blocks of Java programming. They help in performing mathematical operations, comparisons, logical decisions, and bit-level operations. Understanding operators is very important before moving to loops, conditions, and data structures in Java.