Java Reference
In-Depth Information
[JLS 2013]
Chapter 11, “Exceptions”
[Long 2012]
EXP00-J. Do not ignore values returned by methods
53. Do not perform assignments in conditional expressions
Using the assignment operator in conditional expressions frequently indicates program-
mer error and can result in unexpected behavior. The assignment operator should not be
used in the following contexts:
if (controlling expression)
while (controlling expression)
do ... while (controlling expression)
for (second operand)
switch (controlling expression)
?: (first operand)
&& (either operand)
|| (either operand)
?: (second or third operands) where the ternary expression is used in any of these
contexts
Noncompliant Code Example
In this noncompliant code example, the controlling expression in the if statement is an
assignment expression.
Click here to view code image
public void f(boolean a, boolean b) {
if (a = b) {
/* ... */
}
}
Search WWH ::




Custom Search