Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The conditional operator ?: uses the boolean value of one expression its first operand to decide which of the other two expressions should will be evaluated (see JLS, Section 15.25, "Conditional Operator ? :".)

...

The complexity of the rules that determine the result type of a conditional expression can lead to unintended type conversions. Consequently, the second and third operands of each conditional expression should always have the same typeidentical types. This recommendation also applies to boxed primitives.

Noncompliant Code Example

This In this noncompliant code example prints , the programmer expects that both print statements will print the value of alpha as A, which is of the char type. The third operand is a constant expression of type int, whose value 0 can be represented as a char; numeric promotion is unnecessary. However, this behavior depends on the particular value of the constant integer expression; changing that value can lead to different behaviora charA. The first print statement indeed prints A, because the compiler applies the eighth rule in the table above to determine that the second and third operands of the conditional expression are, or are converted to, type char. However, the second print statement prints 65 — the value of alpha as an int. The first matching rule from the table above is the tenth rule; consequently, the compiler promotes the value of alpha to type int.

Code Block
bgColor#FFCCCC
public class Expr {
  public static void main(String[] args) {
    char alpha = 'A';
    int i = 0;
    ... // other code. Value of i may change
    boolean trueExp = ...; // some expression that evaluates to true
    System.out.print(truetrueExp  ? alpha : 0); // prints A
    System.out.print(trueExp ? alpha : 0i); // prints 65
  }
}

Compliant Solution

This compliant solution uses identical types for the second and third operands of the each conditional expression; the explicit cast clarifies casts specify the type expected typeby the programmer.

Code Block
bgColor#ccccff
public class Expr {
  public static void main(String[] args) {
    char alpha = 'A';
    int i = 0;
    boolean trueExp = ...; // Cast 0 as a char to explicitly state that the type of the  some expression that evaluates to true
    System.out.print(trueExp  ? alpha : ((char) 0)); // prints A
    // conditionalIntentional expressiontruncation should be char.of i
    System.out.print(true trueExp ? alpha  : ((char) 0i)); // 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 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

...