Java Reference
In-Depth Information
< Day Day Up >
4. Expression Evaluation
This section concerns aspects of expression evaluation that are not specific to integer or floating-
point arithmetic.
4.1. Mixed-type computations are confusing
Prescription:
Avoid mixed-type computations.
When using the
? :
operator with numeric operands, use the same numeric type for both the second
and third operands.
Prefer constant variables to inline magic numbers.
References:
Puzzles 5
,
8
, and
24
.
4.2. Operands of operators are evaluated left to right
Prescription:
Avoid multiple assignments to the same variable in the same expression. Especially
confusing are multiple compound assignment operators in the same expression.
References:
Puzzles 7
,
25
, and
42
; [JLS 15.7] and [EJ Item 37].
4.3. Operator precedence is not always obvious
Prescription:
Use parentheses, not white space, to make precedence explicit. Replace inline
constant expressions with named constant variables.
References:
Puzzles 11
,
35
, and
95
.
4.4. Operators
==
and
!=
perform reference comparisons on boxed
primitive types
Prescription:
To force a value comparison, assign or cast one operand to the appropriate primitive
type before comparing.
References:
Puzzle 32
; [JLS 15.21, 5.1.8].
4.5. Constant variables are inlined where they are used
Prescription:
Avoid exporting constant fields unless they represents true constants that will never
change.
Search WWH ::
Custom Search