Parentheses explicitly group Java subexpressions, overriding ordinary operator precedence. They are most valuable where precedence is non-obvious to readers: shifts mixed with arithmetic, bitwise operators, conditional expressions, and conditions combining && with ||. Their purpose is not merely to silence a warning; they must express the intended computation. section 2.1 section 2.15
Table 2.1 Operator precedence in Java
|
Precedence
|
Category
|
Operators
|
|---|---|---|
|
1
|
Postfix
|
++,
--
|
|
2
|
Prefix
|
++,
--,
+,
-,
~,
!
|
|
3
|
Multiplicative
|
*,
/,
%
|
|
4
|
Additive
|
+,
-
|
|
5
|
Shift
|
<<,
>>,
>>>
|
|
6
|
Relational
|
<,
>,
<=,
>=,
instanceof
|
|
7
|
Equality
|
==,
!=
|
|
8
|
Bitwise and
|
&
|
|
9
|
Bitwise xor
|
^
|
|
10
|
Bitwise or
|
|
|
|
11
|
Logical and
|
&&
|
|
12
|
Logical or
|
||
|
|
13
|
Conditional
|
?:
|
|
14
|
Assignment
|
=,
+=,
-=,
*=,
/=,
%=,
&=,
^=,
|=,
<<=,
>>=,
>>>=
|
Use the precedence table to check unfamiliar operator combinations, then make important grouping explicit rather than relying on recall. section 2.1
Group shifts before arithmetic
A shift has lower precedence than addition. Thus lo << 16 + hi groups as lo << (16 + hi), not as a packed-value calculation. Write (lo << 16) + hi. Likewise, BLOCK_SIZE + BLOCK_SIZE >> 2 computes (BLOCK_SIZE + BLOCK_SIZE) >> 2, yielding half the block size rather than a 25% increase; write BLOCK_SIZE + (BLOCK_SIZE >> 2). Where suitable, multiplication or division can state the calculation more plainly. section 2.1.1
Parentheses can also document bitwise intent. bits & 0xFF00 + 1 groups as bits & 0xFF01, so it copies the low bit instead of setting it. Prefer (bits & 0xFF00) | 1. Better still, avoid mixing bitwise and arithmetic operations when one expression needlessly obscures the result. section 2.1.2
Keep a conditional expression together
The conditional operator has very low precedence. In str.length() + indent < 0 ? 0 : indent, the comparison is evaluated before ?:, so the capacity can become a negative indent and cause NegativeArraySizeException. The intended calculation is str.length() + Math.max(indent, 0); when a conditional is embedded in a larger expression, wrap it in parentheses. section 2.2.2
Separate arithmetic from string construction
The + operator associates left to right and becomes string concatenation after a String operand. "Entry#" + index + 1 therefore produces Entry#41 when index is 4. Write "Entry#" + (index + 1), calculate an adjustedIndex first, or use String.format or MessageFormat. section 2.3
Use parentheses and simpler structure to prevent precedence mistakes:
- Group operands whenever shifts appear with addition or subtraction; consider multiplication or division instead of shifts when that makes the calculation clearer. section 2.1.1
- Wrap a negated instanceof test as !(obj instanceof String), and group bitwise flag alternatives before comparing them. section 2.2
- When && and || occur together, group the intended alternatives or split the condition into nested if statements or a named predicate. section 2.2.1
- Put arithmetic inside parentheses during string concatenation, even when the grouping is redundant enough to serve as documentation. section 2.3
- Treat static-analysis quick fixes as prompts for review: added parentheses can preserve an existing bug rather than correct its intended semantics. section 2.1.1 section 2.1.2
Compiler errors catch several malformed condition expressions, but valid precedence errors can remain silent. Static-analysis inspections can flag mixed-precedence expressions, yet warnings may be noisy or context-dependent. For complex conditions, reason through inputs and add tests that exercise every operand for both true and false results. section 2.2 section 2.2.2 section 2.2.1

