An expression has a type and a value (and may have a side effect). A statement does something. n + 1 is an expression. n = n + 1; is a statement that contains expressions.
++, assignment, method calls) make order of evaluation matter.Definition: operator precedence is how the parser groups tokens. Associativity decides grouping when two operators share a precedence (almost all are left-associative; assignment and the ternary are right-associative).
| Op | Meaning | Notes |
|---|---|---|
+ - * / % | Add, sub, mul, div, remainder | Integer / truncates toward zero. % result sign follows the dividend. |
+ on String | Concatenation | If either operand is String, + is concat, not add. |
5 / 2 // 2 5 % 2 // 1 -5 % 2 // -1 5 / 0 // ArithmeticException (int) 5.0 / 0 // Infinity (IEEE) Integer.MAX_VALUE + 1 // MIN_VALUE — silent wrap
int before + - * / %.long y = 2_000_000_000 * 2; still multiplies as int. Widen first: 2_000_000_000L * 2.Math.addExact / multiplyExact when wrap is a defect.< > <= >= compare numbers (and chars as code units). Result type is boolean.== / != on primitives: value. On references: identity (same object), not content.0 < n < 10. Chain with &&.== on double is almost always wrong (NaN, rounding). Compare with a tolerance or use integers (cents).// Incorrect
if (name == "admin") { }
if (0.1 + 0.2 == 0.3) { }
// Correct
if ("admin".equals(name)) { }
if (Math.abs(a - b) < 1e-9) { } // only if you truly mean approximate
| Op | Name | Short-circuit? |
|---|---|---|
&& | Conditional AND | Yes — right side skipped if left is false |
|| | Conditional OR | Yes — right side skipped if left is true |
& | on boolean | Non-short-circuit | No — both sides always run |
! | NOT | — |
^ on boolean | XOR | No |
if (s != null && s.length() > 0) { } // safe
if (s != null & s.length() > 0) { } // NPE when s is null
When to use &&: almost always. When & on boolean is intentional: you need both side effects. That is rare and should look deliberate.
On integers: & | ^ ~ << >> >>>. These are not the same as && ||.
<< left shift. >> arithmetic right (sign-extends). >>> logical right (zero-fills).int, only the low 5 bits of the distance count (1 << 32 is 1, not 0).int flags = READ | WRITE; boolean canRead = (flags & READ) != 0; int x = -1; x >> 1; // still negative (sign bit copied) x >>> 1; // large positive
int n = 5; int a = n++; // a=5, n=6 (post: yield old, then add) int b = ++n; // n=7, b=7 (pre: add, then yield new) n += 2; // n = (n + 2) with a hidden cast if n is byte/short int m = cond ? 1 : 0;
++ inside a larger expression you also read. Exam writers love a[i++] = i++. Professionals write two lines.+= includes a cast back to the left type: byte b = 1; b += 1; compiles; b = b + 1; does not.int and double yields double. Mixing a primitive and a wrapper can box unexpectedly.if ((n = next()) > 0) is legal and easy to misread as ==.Highest to lower (selected): postfix ++ → unary ++ ! - → multiplicative → additive → shifts → relational → == → & → ^ → | → && → || → ternary → assignment.
a + b * c // a + (b * c) n << 2 + 1 // n << (2 + 1), not (n << 2) + 1 flag = a == b // flag = (a == b) a & b == c // a & (b == c) — usually a bug; parenthesize
When two readings are possible, use parentheses. Precedence trivia is not a style guide.
= instead of == in a condition — compiles when the assignment yields boolean.+ next to a String silently concatenates numbers: "id=" + 1 + 2 is id=12.& when you meant && (NPE, extra work, extra side effects).double money with ==.n++ + ++n has a portable “obvious” value — do not write it.boolean for comparisons, String concat rules.== is identity. Content uses equals.&& / ||. Reserve bitwise ops for bits.++ inside a busy line.~ Good luck with your revision! ~