Operators
Operators are the built-in symbols that combine values into expressions: arithmetic like +, comparison like <, logic like &&, and bit manipulation like <<.
Detailed Explanation
An operator takes one, two, or three operands and produces a value. Java's set is fixed. Unlike C++, Java has no operator overloading, so + means numeric addition or string concatenation and nothing else, ever. That restriction is a deliberate readability decision: when you see a + b in Java you know it is not calling user code.
The groups worth knowing are these.
Arithmetic: + - * / %. Integer division truncates toward zero, so 7 / 2 is 3, and dividing an integer by zero throws ArithmeticException. Floating point division by zero does not throw; it yields Infinity or NaN. The % operator takes the sign of the left operand, so -7 % 3 is -1, which differs from Python.
Comparison: < <= > >= == !=. On primitives these compare values. On references, == compares identity, which is the single most common source of bugs with String and boxed types.
Logical: && || !. These short-circuit: the right operand is not evaluated if the left settles the answer. That is what makes if (user != null && user.isActive()) safe. The non-short-circuiting & and | also work on booleans and always evaluate both sides, which matters if the right side has side effects.
Bitwise and shift: & | ^ ~ << >> >>>. Java has no unsigned integer types, so it provides >>>, an unsigned right shift that fills with zeros, alongside >>, which preserves the sign bit. This is why HashMap internally uses h ^ (h >>> 16) to spread hash bits.
Assignment: = and the compound forms += -= *= /= %= and the bitwise equivalents. Compound assignment includes a hidden narrowing cast. byte b = 10; b += 300; compiles and silently wraps, while b = b + 300; does not compile at all.
Ternary: condition ? a : b is Java's only conditional expression. It is genuinely useful for short choices and unreadable when nested.
instanceof: since Java 16 it binds a pattern variable, so if (shape instanceof Circle c) tests and casts in one step.
Precedence follows the familiar mathematical order, with unary highest, then multiplicative, additive, shift, relational, equality, bitwise, logical, ternary, assignment. Shift binds looser than addition, which surprises people: 1 << 2 + 3 is 1 << 5, not 4 + 3. Use parentheses rather than trusting the table.
Evaluation order is fully specified in Java, unlike C. Operands evaluate left to right, and all operands are evaluated before the operator is applied. This removes the undefined behaviour that i = i++ + ++i has in C, although the Java result is still a terrible thing to write.
The costs are almost entirely about correctness rather than speed. Precedence mistakes, silent narrowing in compound assignment, and == on references produce wrong answers, not slow ones. The one performance-relevant point is that % and / on integers are meaningfully slower than shifts and masks, which is why bit tricks still appear in hash tables and ring buffers.
Code Examples
public class OperatorBehaviour {
public static void main(String[] args) {
System.out.println(-7 % 3); // prints: -1 — sign follows the left operand
System.out.println(1.0 / 0); // prints: Infinity — no exception for doubles
// System.out.println(1 / 0); // would throw ArithmeticException: / by zero
int negative = -8;
System.out.println(negative >> 1); // prints: -4 — sign bit is preserved
System.out.println(negative >>> 28); // prints: 15 — zeros shifted in from the left
System.out.println(1 << 2 + 3); // prints: 32 — '+' binds tighter than '<<'
System.out.println((1 << 2) + 3); // prints: 7
byte small = 10;
small += 300; // compiles: compound assignment casts implicitly
System.out.println(small); // prints: 54 — silent truncation
// small = small + 300; // does not compile: int cannot fit in byte
}
}public class ShortCircuitAndTernary {
record User(String name, boolean active) { }
static boolean audit(String message) {
System.out.println("audited: " + message);
return true;
}
public static void main(String[] args) {
User user = null;
// && stops at the first false, so isActive() is never called on null.
if (user != null && user.active()) {
System.out.println("active");
}
// & evaluates both sides, so this WOULD throw if user were dereferenced.
boolean flagged = false & audit("still runs");
// prints: audited: still runs
System.out.println(flagged); // prints: false
int retries = 3;
String label = retries == 1 ? "1 retry" : retries + " retries";
System.out.println(label); // prints: 3 retries
Object shape = "circle";
// Pattern matching for instanceof (Java 16+): test and bind in one step.
if (shape instanceof String s && s.length() > 3) {
System.out.println(s.toUpperCase()); // prints: CIRCLE
}
}
}Common Gotchas
Compound assignment hides a narrowing cast
`byte`, `short`, and `char` variables accept `+=` with an `int` and truncate the result without any warning. Spell out the arithmetic when the type is narrow.
`==` compares references for objects
`"abc" == new String("abc")` is `false`. This also bites with `Integer` above 127, where the cache stops applying. Use `equals`.
Integer division by zero throws, floating point does not
`1 / 0` throws `ArithmeticException`; `1.0 / 0` produces `Infinity` and `0.0 / 0` produces `NaN`. `NaN != NaN` is `true`, so test with `Double.isNaN`.
Java has no operator overloading
`+` on `String` is the only special case, and it is compiled into `StringBuilder` or `invokedynamic` calls. `BigDecimal` arithmetic must be written as `a.add(b)`, not `a + b`.