Memra

Operators & precedence

Arithmetic, comparison, logical short-circuit, and the ternary.

Working with values

Arithmetic: + - * / and % (remainder). Integer division truncates: 7 / 2 is 3, and 7 % 2 is 1. Use a double operand to get 3.5.

Comparison: == != < > <= >= produce a boolean.

Logical: && (and), || (or), ! (not). && and || short-circuit — they stop evaluating as soon as the answer is known. a() && b() never calls b() if a() is false. This is what makes s != null && s.length() > 0 safe.

Ternary: condition ? whenTrue : whenFalse is an expression that picks one of two values:

int max = (a > b) ? a : b;

Precedence roughly: unary > * / % > + - > comparisons > && > || > assignment. When in doubt, parenthesise — clarity beats cleverness.

NORMAL ~/memra/learn/java-from-zero/operators-and-precedence utf-8 LF