Operators are symbols used to perform operations on variables and values.
Java supports several types of operators grouped by function.
🔢 Arithmetic Operators
Used to perform basic mathematical operations like addition, subtraction, etc.
Operator | Meaning | Example |
|---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Modulus (remainder) |
|
🔸 Code:
int a = 15, b = 4;
System.out.println("The value of a = " + a);
System.out.println("The value of b = " + 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));
System.out.println("a % b = " + (a % b));The value of a = 15
The value of b = 4
a + b = 19
a - b = 11
a * b = 60
a / b = 3
a % b = 3🔍 Why use them?
To perform calculations like billing, scores, measurements, etc.
🟰🟰 Relational (Comparison) Operators
Used to compare two values. Always returns a boolean (true or false).
Operator | Meaning | Example |
|---|---|---|
| Equal to |
|
| Not equal to |
|
| Greater than |
|
| Less than |
|
| Greater or equal |
|
| Less or equal |
|
🔸 Code:
int x = 10, y = 20;
System.out.println("The value of x = " + x);
System.out.println("The value of y = " + y);
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x >= 10: " + (x >= 10));
System.out.println("y <= 15: " + (y <= 15));The value of x = 10
The value of y = 20
x == y: false
x != y: true
x > y: false
x < y: true
x >= 10: true
y <= 15: false🔍 Why use them?
Used in decisions and conditions, like checking age, passwords, values, etc.
❕Logical Operators
Used to combine boolean expressions. Commonly used in conditions and loops.
Operator | Meaning | Example |
|---|---|---|
| Logical AND |
|
| Logical NOT |
|
` | ` |
🔸 Code:
int age = 25;
boolean hasID = true;
System.out.println("Can enter? " + (age >= 18 && hasID));
System.out.println("Underage or no ID? " + (age < 18 || !hasID));
Can enter? true
Underage or no ID? false🔍 Why use them?
Used in if-else conditions, loops, filters, etc.
🟰 Assignment Operators
Used to assign values to variables.
Operator | Meaning | Example |
|---|---|---|
| Assign |
|
| Add and assign |
|
| Subtract and assign |
|
| Multiply and assign |
|
| Divide and assign |
|
| Modulo and assign |
|
🔸 Code:
public class LogicalOperatorsDemo {
public static void main(String[] args) {
int age = 20;
boolean hasID = true;
boolean isMember = false;
// Logical AND: Only true if both conditions are true
boolean canEnter = (age >= 18 && hasID);
// true && true → true
// Logical OR: True if at least one condition is true
boolean getsDiscount = (age < 18 || isMember);
// false || false → false
// Logical NOT: Reverses the result
boolean noID = !hasID;
// !true → false
// Output results
System.out.println("Age: " + age);
System.out.println("Has ID: " + hasID);
System.out.println("Is a member: " + isMember);
System.out.println("Can enter: " + canEnter); // true
System.out.println("Gets discount: " + getsDiscount); // false
System.out.println("Doesn't have ID: " + noID); // false
}
}Age: 20
Has ID: true
Is a member: false
Can enter: true
Gets discount: false
Doesn't have ID: false🔍 Why use them?
They simplify and shorten mathematical updates to variables.
➕ Unary Operators
Operate on only one operand.
Operator | Meaning | Example |
|---|---|---|
| Unary plus (no effect) |
|
| Unary minus |
|
| Increment |
|
| Decrement |
|
| Logical NOT |
|
🔸 Code:
public class UnaryOperatorsDemo {
public static void main(String[] args) {
int a = 5;
boolean flag = true;
System.out.println("Initial value of a: " + a);
// Unary plus
System.out.println("Unary plus (+a): " + (+a)); // Output: 5
// Unary minus
System.out.println("Unary minus (-a): " + (-a)); // Output: -5
// Pre-increment
System.out.println("Pre-increment (++a): " + (++a)); // Output: 6
// Post-increment
System.out.println("Post-increment (a++): " + (a++)); // Output: 6 (then a becomes 7)
System.out.println("Value after post-increment: " + a); // Output: 7
// Pre-decrement
System.out.println("Pre-decrement (--a): " + (--a)); // Output: 6
// Post-decrement
System.out.println("Post-decrement (a--): " + (a--)); // Output: 6 (then a becomes 5)
System.out.println("Value after post-decrement: " + a); // Output: 5
// Logical NOT
System.out.println("Logical NOT (!flag): " + (!flag)); // Output: false
// Bitwise complement
System.out.println("Bitwise complement (~a): " + (~a)); // Output: -6
}
}Initial value of a: 5
Unary plus (+a): 5
Unary minus (-a): -5
Pre-increment (++a): 6
Post-increment (a++): 6
Value after post-increment: 7
Pre-decrement (--a): 6
Post-decrement (a--): 6
Value after post-decrement: 5
Logical NOT (!flag): false
Bitwise complement (~a): -6🔍 Why use them?
Unary operators are used to perform simple, quick operations on a single operand. They’re helpful in writing cleaner, shorter, and more expressive code.