Working with Operators and Expressions
1. Using Arithmetic Operators (+, -, *, /, %)
| Operator | Description | Notes |
+ | Addition / String concat | Promotes to wider type |
- | Subtraction | |
* | Multiplication | May overflow silently |
/ | Division | Integer / integer truncates |
% | Modulus | Sign matches dividend |
Warning: int / 0 throws ArithmeticException; double / 0 yields Infinity or NaN.
2. Using Unary Operators (++, --, +, -, !)
| Operator | Position | Effect |
++x | Prefix | Increment, returns new value |
x++ | Postfix | Returns old value, then increments |
--x / x-- | Pre/Post | Decrement |
+ / - | Prefix | Sign |
! | Prefix | Logical NOT (boolean only) |
~ | Prefix | Bitwise complement |
3. Using Relational Operators (==, !=, >, <, >=, <=)
| Operator | Use |
== | Equal (reference equality for objects) |
!= | Not equal |
> < >= <= | Numeric comparison only |
Warning: Use .equals() for object value comparison; == compares references.
4. Using Logical Operators (&&, ||, !)
| Operator | Description | Short-Circuit |
&& | Logical AND | Yes |
|| | Logical OR | Yes |
! | Logical NOT | — |
& | | Boolean AND/OR (no short-circuit) | No |
^ | Boolean XOR | No |
5. Using Bitwise Operators (&, |, ^, ~, <<, >>, >>>)
| Operator | Action | Example |
& | AND | 0b1100 & 0b1010 = 0b1000 |
| | OR | 0b1100 | 0b1010 = 0b1110 |
^ | XOR | 0b1100 ^ 0b1010 = 0b0110 |
~ | NOT | ~5 = -6 |
<< | Left shift | 1 << 3 = 8 |
>> | Signed right shift | Preserves sign bit |
>>> | Unsigned right shift | Fills with 0 |
6. Using Assignment Operators (=, +=, -=, *=, /=)
| Operator | Equivalent |
x += y | x = (T)(x + y) (implicit cast) |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
x %= y | x = x % y |
&= |= ^= <<= >>= >>>= | Compound bitwise |
7. Using Ternary Operator (? :)
Example: Ternary
String label = (count == 1) ? "item" : "items";
int max = (a > b) ? a : b;
String status = user.isActive() ? "online" : user.isAway() ? "away" : "offline";
| Aspect | Detail |
| Syntax | condition ? exprIfTrue : exprIfFalse |
| Result type | Common supertype of both branches |
8. Understanding Operator Precedence
| Precedence | Operators |
| 1 (highest) | () [] . postfix ++ -- |
| 2 | prefix ++ -- + - ! ~ (cast) |
| 3 | * / % |
| 4 | + - |
| 5 | << >> >>> |
| 6 | < > <= >= instanceof |
| 7 | == != |
| 8-10 | &, ^, | |
| 11-12 | &&, || |
| 13 | ?: |
| 14 (lowest) | = += -= *= ... |
9. Using instanceof Operator
Example: Pattern matching for instanceof JAVA 16+
Object obj = "hello";
if (obj instanceof String s) {
System.out.println(s.length()); // s is auto-cast
}
| Form | Behavior |
x instanceof T | true if x is non-null T or subtype |
x instanceof T t | Pattern variable binding (Java 16+) |
null instanceof T | Always false |
10. Understanding Short-Circuit Evaluation
| Operator | Stops When |
&& | Left side is false |
|| | Left side is true |
Example: Null-safe check
if (user != null && user.isActive()) { ... } // safe
if (cache != null || (cache = loadCache()) != null) { ... }