In Java, operators are special symbols that perform operations on variables and values.
They can be grouped into several categories:
1. Arithmetic Operators
Used for basic mathematical operations.
Operator | Description | Example (a=10 , b=3 ) | Result |
---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division | a / b | 3 |
% | Modulus (remainder) | a % b | 1 |
++ | Increment (by 1) | a++ | 11 |
-- | Decrement (by 1) | a-- | 9 |
2. Relational Operators
Used to compare two values (result is true
or false
).
Operator | Meaning | Example (a=10 , b=3 ) | Result |
---|---|---|---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | true |
< | Less than | a < b | false |
>= | Greater than or equal to | a >= b | true |
<= | Less than or equal to | a <= b | false |
3. Logical Operators
Used to combine conditions.
Operator | Meaning | Example | Result |
---|---|---|---|
&& | Logical AND | (a > b) && (a < 20) | true |
|| | Logical OR | (a < b) || (a < 20) | true |
! | Logical NOT | !(a > b) | false |
4. Assignment Operators
Used to assign values.
Operator | Example | Equivalent to |
---|---|---|
= | a = 5 | a = 5 |
+= | a += 3 | a = a + 3 |
-= | a -= 3 | a = a – 3 |
*= | a *= 3 | a = a * 3 |
/= | a /= 3 | a = a / 3 |
%= | a %= 3 | a = a % 3 |
5. Unary Operators
Operate on a single operand.
Operator | Description |
---|---|
+ | Positive (no change) |
- | Negation |
++ | Increment |
-- | Decrement |
! | Logical NOT |
6. Bitwise Operators
Operate on bits.
Operator | Meaning | Example (a=5 , b=3 ) | Binary Result |
---|---|---|---|
& | AND | a & b | 1 (0001 ) |
| | OR | a | b | 7 (0111 ) |
^ | XOR | a ^ b | 6 (0110 ) |
~ | Bitwise NOT | ~a | -6 |
<< | Left shift | a << 1 | 10 (1010 ) |
>> | Right shift | a >> 1 | 2 (0010 ) |
>>> | Unsigned right shift | a >>> 1 | 2 |
7. Ternary Operator
A shorthand for if-else
.
int result = (a > b) ? a : b; // If a>b, result=a else result=b
8. Instanceof Operator
Checks if an object is an instance of a specific class.
if (obj instanceof String) { ... }
If you want, I can make you a Java table with all operators, precedence, and associativity so you know the exact order they get evaluated. That’s very useful for coding interviews and tricky expressions. Would you like me to prepare that?
In Java, both i++
and ++i
increase the value of i
by 1, but the difference is when the increment happens in relation to the expression’s evaluation.
1. i++
→ Post-increment
- Increment happens after the current value is used in the expression.
- Steps:
- Take the current value of
i
and use it. - Then increment
i
by 1.
- Take the current value of
Example:
int i = 5;
int x = i++; // x = 5, i = 6
Here:
x
gets the old value (5).i
is incremented afterwards (now 6).
2. ++i
→ Pre-increment
- Increment happens before the value is used in the expression.
- Steps:
- Increment
i
by 1. - Use the new value.
- Increment
Example:
int i = 5;
int x = ++i; // x = 6, i = 6
Here:
i
increments first (becomes 6).x
gets the new value (6).
3. Side-by-side comparison
Expression | Increment timing | Value returned | Final i value |
---|---|---|---|
i++ | After value is used | Old value | Old value + 1 |
++i | Before value is used | New value | Old value + 1 |
4. Example inside a loop
for (int i = 0; i < 3; i++) {
System.out.println(i); // prints 0, 1, 2
}
Here i++
increments after each loop iteration’s body runs.
If you used ++i
here, the loop’s behavior would be the same because the increment happens at the end anyway — the difference only matters inside expressions where the increment and usage happen together.