The Conditional Operator ?: – Basic Elements, Primitive Data Types, and Operators

2.18 The Conditional Operator ?:

The ternary conditional operator ?: allows conditional expressions to be defined. The conditional expression has the following syntax:

condition
 ?
expression
1
 :
expression
2

It is called ternary because it has three operands. If the boolean expression condition is true, then expression1 is evaluated; otherwise, expression2 is evaluated. Both expression1 and expression2 must evaluate to values that can be converted to the type of the conditional expression. This type is determined from the types of the two expressions. The value of the evaluated expression is converted to the type of the conditional expression, and may involve autoboxing and unboxing.

Evaluation of a conditional expression is an example of short-circuit evaluation. As only one of the two expressions is evaluated, one should be wary of any side effects in a conditional expression.

In the following code snippet at (1), both expressions in the conditional expression are of type byte. The type of the conditional expression is therefore byte. That a value of type byte can be converted to an int by an implicit widening numeric conversion to be assignment compatible with the int variable daysInFebruary is secondary in determining the type of the conditional expression. Note that the conditional operator at (1) has higher precedence than the assignment operator =, making it unnecessary to enclose the conditional expression in parentheses.

Click here to view code image

boolean leapYear = false;
byte v29 = 29;
byte v28 = 28;
int daysInFebruary = leapYear ? v29 : v28;   // (1)

The following examples illustrate the use of conditional expressions. The type of the conditional expression at (2) is int, and no conversion of any expression value is necessary. The type of the conditional expression at (3) is double, due to binary numeric promotion: The int value of the first expression is promoted to a double. The compiler reports an error because a double cannot be assigned to an int variable. The type of the conditional expression at (4) is also double as at (3), but now the double value is assignment compatible with the double variable minDoubleValue.

Click here to view code image

int i = 3;
int j = 4;
int minValue1 = i < j ? i : j;                        // (2) int
int minValue2 = i < j ? i : Double.MIN_VALUE;         // (3) double. Not OK.
double minDoubleValue = i < j ? i : Double.MIN_VALUE; // (4) double

At (5) below, the primitive values of the expressions can be boxed and assigned to an Object reference. At (6), the int value of the first expression can be boxed in an Integer. The println() method creates and prints a text representation of any object whose reference value is passed as a parameter.

Click here to view code image

// Assume i and j are of type int and initialized correctly.
Object obj = i < j ? i : true;        // (5) value of i boxed in Integer or
                                      //     literal true boxed in Boolean
System.out.println(i < j ? i : “Hi”); // (6) value of i boxed in Integer or
                                      //     String object “Hi”

The conditional expression is not an expression statement. The following code will not compile:

Click here to view code image

(i < j) ? i : j;    // Compile-time error!

The conditional expression can be nested, and the conditional operator associates from right to left.

Click here to view code image

a?b:c?d:e?f:g evaluates as (a?b:(c?d:(e?f:g)))

The value of this conditional expression is g if, and only if, a, c, and e are false. A nested conditional expression is used in the next example. As a convention, the condition in a conditional expression is enclosed in parentheses to aid in reading the code. Typically, a conditional expression is used when it makes the code easier to read, especially when the expressions are short and without side effects.

Click here to view code image

int n = 3;
String msg = (n==0) ? “no cookies.” : (n==1) ? “one cookie.” : “many cookies.”;
System.out.println(“You get ” + msg); // You get many cookies.

The conditional operator is the expression equivalent of the if-else statement (ยง4.1, p. 153).