...
Code Block |
---|
|
public class Expr {
public static void main(String[] args) {
char alpha = 'A';
int i = 0;
/* other code. Value of i may change */
boolean trueExp = ...; // set to some expression that evaluates to true
System.out.print(trueExp ? alpha : 0); // prints A
System.out.print(trueExp ? alpha : i); // prints 65
}
}
|
...
Code Block |
---|
|
public class Expr {
public static void main(String[] args) {
char alpha = 'A';
int i = 0;
boolean trueExp = ...; // some expression that evaluates to true
System.out.print(trueExp ? alpha : ((char) 0)); // prints A
// Deliberate Intentionalnarrowing truncationcast of i; possible truncation OK
System.out.print(trueExp ? alpha : ((char) i)); // prints A
}
}
|
Note that the explicit cast in the first conditional expression is redundant; that is, the value printed remains identical whether the cast is present or absent. Nevertheless, use of the redundant cast is good practice; it serves as an explicit indication of the programmer's intent, and consequently improves maintainability. When the value of i
in the second conditional expression falls outside the range that can be represented as a char
, the explicit cast will truncate its value. Although this appears to violate This usage complies with exception EXP13-EX1 of guideline EXP13-J. Do not cast numeric types to narrower types without a range check, in this case the truncation is both intended and also documented as intended. Consequently, this code falls under exception EXP13-EX1 and conforms with the guideline.
Noncompliant Code Example
This noncompliant code example prints {{65}}â”the ASCII equivalent of A
, instead of the expected A
, because the second operand (alpha
) must be promoted to type int
. The numeric promotion occurs because the value of the third operand (the constant expression '123456') is too large to be represented as a char
100 as the size of the HashSet rather than the expected result (some value between 0 and 50). The combination of values of types short
and int
in the second argument of the conditional expression (the operation i-1
) causes the result to be an int
as specified by the normal integer promotion rules. Consequently, the Short
object in the third argument is autounboxed into a short
, which is then promoted into an int
. The result of the conditional expression is then autoboxed into an object of type Integer
. Because the HashSet
contains only values of type Short
, the call to HashSet.remove()
has no effect.
Code Block |
---|
|
public class ExprShortSet {
public static void main(String[] args) {
charHashSet<Short> alphas = 'A' new HashSet<Short>();
System.out.print(true ? alpha : 123456);
}
}
|
Compliant Solution
The compliant solution explicitly states the intended result type by casting alpha
to type int
. Casting 123456
to type char
would ensure that both operands of the conditional expression have the same type, resulting in A
being printed. However, it would result in data loss when an integer larger than Character.MAX_VALUE
is downcast to type char
. This compliant solution avoids potential truncation by casting alpha
to type int
, the wider of the operand types.
Code Block |
---|
|
public class Expr {
public static void main(String[] args) { for (short i = 0; i < 100; i++) {
s.add(i);
char alpha = 'A';
// Cast alphaof asi-1 anis intsafe, tobecause explicitlyvalue stateis thatalways therepresentable
type of the
//Short conditionalworkingVal expression= should be int.
(short) (i-1);
System.out.print(true ? ((int) alpha) : 123456);
}
}
|
Noncompliant Code Example
This noncompliant code example prints 65
instead of A
. The third operand is a variable of type int
, so the second operand (alpha
) must be converted to type int
.
Code Block |
---|
|
public class Expr {
public static void main(String[] args) {
char alpha = 'A';
int i = 0; ... // other code may update workingVal
s.remove(((i & 1) == 0) ? i-1 : workingVal);
}
System.out.print(true ? alpha : iprintln(s.size());
}
}
|
Compliant Solution
This compliant solution declares i
as type char
, ensuring that casts the second operant to type short
, then explicitly invokes the Short.valueOf
method to create a Short
instance whose value is i - 1
. Consequently, the second and third operands of the conditional expression both have the same typetype Short
, and the remove()
call has the expected effect.
Code Block |
---|
|
public class ExprShortSet {
public static void main(String[] args) {
charHashSet<Short> alphas = 'A'new HashSet<Short>();
char for (short i = 0; //declare as char i < 100; i++) {
System.out.print(true ? alpha : s.add(i);
}
}
|
Noncompliant Code Example
...
...
...
...
...
...
...
...
...
...
...
...
Code Block |
---|
|
public class Expr {
public static void main(String[] args) {
Integer i = Integer.MAX_VALUE;
float f = 0;
System.out.print(true ? i : f);
}
}
|
Compliant Solution
This compliant solution declares both the operands as Integer
.
Code Block |
---|
|
public class Expr {
public static void main(String[] args) {
Integer i = Integer.MAX_VALUE;
Integer f = 0; //declare as Integerbecause value is always representable
Short workingVal = (short) (i-1);
... // other code may update workingVal
// Cast of i-1 is safe, because value is always representable
s.remove(((i & 1) == 0) ? Short.valueOf((short) (i-1)) : workingVal);
}
System.out.print(true ? i : fprintln(s.size());
}
}
|
Writing the conditional expression as ((i & 1) == 0) ? (short) (i-1)) : workingVal
also complies with this guideline, because both the second and third operands in this form have type short
. However, this alternative is less efficient, because it forces both autounboxing of workingVal
on each even iteration of the loop and also autoboxing of the result of the conditional expression (from short
to Short
) on every iteration of the loop.
Risk Assessment
When the second and third operands of a conditional expression have different types, they can be subject to type conversions that were not anticipated by the programmer.
...