JVM Knowledge

The conditional operator ? : uses the boolean value of one expression to decide which of two other expressions should be evaluated.

ConditionalExpression:
  ConditionalOrExpression
  ConditionalOrExpression ? Expression : ConditionalExpression
  ConditionalOrExpression ? Expression : LambdaExpression

The conditional operator is syntactically right-associative (it groups right-to-left). Thus, a?b:c?d:e?f:g means the same as a?b:(c?d:(e?f:g)).

The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.

The first expression must be of type boolean or Boolean, or a compile-time error occurs.

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.

There are three kinds of conditional expressions, classified according to the second and third operand expressions: boolean conditional expressions, numeric conditional expressions, and reference conditional expressions. The classification rules are as follows:

  • If both the second and the third operand expressions are boolean expressions, the conditional expression is a boolean conditional expression.

    For the purpose of classifying a conditional, the following expressions are boolean expressions:

    • An expression of a standalone form (§15.2) that has type boolean or Boolean.

    • A parenthesized boolean expression (§15.8.5).

    • A class instance creation expression (§15.9) for class Boolean.

    • A method invocation expression (§15.12) for which the chosen most specific method (§15.12.2.5) has return type boolean or Boolean.

      Note that, for a generic method, this is the type before instantiating the method's type arguments.

    • A boolean conditional expression.

  • If both the second and the third operand expressions are numeric expressions, the conditional expression is a numeric conditional expression.

    For the purpose of classifying a conditional, the following expressions are numeric expressions:

    • An expression of a standalone form (§15.2) with a type that is convertible to a numeric type (§4.2, §5.1.8).
    • A parenthesized numeric expression (§15.8.5).
    • A class instance creation expression (§15.9) for a class that is convertible to a numeric type.
    • A method invocation expression (§15.12) for which the chosen most specific method (§15.12.2.5) has a return type that is convertible to a numeric type.
    • A numeric conditional expression.
  • Otherwise, the conditional expression is a reference conditional expression.

The process for determining the type of a conditional expression depends on the kind of conditional expression, as outlined in the following sections.

The following tables summarize the rules above by giving the type of a conditional expression for all possible types of its second and third operands. bnp(..) means to apply binary numeric promotion. The form "T | bnp(..)" is used where one operand is a constant expression of type int and may be representable in type T, where binary numeric promotion is used if the operand is not representable in type T. The operand type Object means any reference type other than the null type and the eight wrapper classes Boolean, Byte, Short, Character, Integer, Long, Float, Double.

Table 15.25-A. Conditional expression type (Primitive 3rd operand, Part I)

3rd → byte short char int
2nd ↓
byte byte short bnp(byte,char) byte | bnp(byte,int)
Byte byte short bnp(Byte,char) byte | bnp(Byte,int)
short short short bnp(short,char) short | bnp(short,int)
Short short short bnp(Short,char) short | bnp(Short,int)
char bnp(char,byte) bnp(char,short) char char | bnp(char,int)
Character bnp(Character,byte) bnp(Character,short) char char | bnp(Character,int)
int byte | bnp(int,byte) short | bnp(int,short) char | bnp(int,char) int
Integer bnp(Integer,byte) bnp(Integer,short) bnp(Integer,char) int
long bnp(long,byte) bnp(long,short) bnp(long,char) bnp(long,int)
Long bnp(Long,byte) bnp(Long,short) bnp(Long,char) bnp(Long,int)
float bnp(float,byte) bnp(float,short) bnp(float,char) bnp(float,int)
Float bnp(Float,byte) bnp(Float,short) bnp(Float,char) bnp(Float,int)
double bnp(double,byte) bnp(double,short) bnp(double,char) bnp(double,int)
Double bnp(Double,byte) bnp(Double,short) bnp(Double,char) bnp(Double,int)
boolean lub(Boolean,Byte) lub(Boolean,Short) lub(Boolean,Character) lub(Boolean,Integer)
Boolean lub(Boolean,Byte) lub(Boolean,Short) lub(Boolean,Character) lub(Boolean,Integer)
null lub(null,Byte) lub(null,Short) lub(null,Character) lub(null,Integer)
Object lub(Object,Byte) lub(Object,Short) lub(Object,Character) lub(Object,Integer)

Table 15.25-B. Conditional expression type (Primitive 3rd operand, Part II)

3rd → long float double boolean
2nd ↓
byte bnp(byte,long) bnp(byte,float) bnp(byte,double) lub(Byte,Boolean)
Byte bnp(Byte,long) bnp(Byte,float) bnp(Byte,double) lub(Byte,Boolean)
short bnp(short,long) bnp(short,float) bnp(short,double) lub(Short,Boolean)
Short bnp(Short,long) bnp(Short,float) bnp(Short,double) lub(Short,Boolean)
char bnp(char,long) bnp(char,float) bnp(char,double) lub(Character,Boolean)
Character bnp(Character,long) bnp(Character,float) bnp(Character,double) lub(Character,Boolean)
int bnp(int,long) bnp(int,float) bnp(int,double) lub(Integer,Boolean)
Integer bnp(Integer,long) bnp(Integer,float) bnp(Integer,double) lub(Integer,Boolean)
long long bnp(long,float) bnp(long,double) lub(Long,Boolean)
Long long bnp(Long,float) bnp(Long,double) lub(Long,Boolean)
float bnp(float,long) float bnp(float,double) lub(Float,Boolean)
Float bnp(Float,long) float bnp(Float,double) lub(Float,Boolean)
double bnp(double,long) bnp(double,float) double lub(Double,Boolean)
Double bnp(Double,long) bnp(Double,float) double lub(Double,Boolean)
boolean lub(Boolean,Long) lub(Boolean,Float) lub(Boolean,Double) boolean
Boolean lub(Boolean,Long) lub(Boolean,Float) lub(Boolean,Double) boolean
null lub(null,Long) lub(null,Float) lub(null,Double) lub(null,Boolean)
Object lub(Object,Long) lub(Object,Float) lub(Object,Double) lub(Object,Boolean)

Table 15.25-C. Conditional expression type (Reference 3rd operand, Part I)

3rd → Byte Short Character Integer
2nd ↓
byte byte short bnp(byte,Character) bnp(byte,Integer)
Byte Byte short bnp(Byte,Character) bnp(Byte,Integer)
short short short bnp(short,Character) bnp(short,Integer)
Short short Short bnp(Short,Character) bnp(Short,Integer)
char bnp(char,Byte) bnp(char,Short) char bnp(char,Integer)
Character bnp(Character,Byte) bnp(Character,Short) Character bnp(Character,Integer)
int byte | bnp(int,Byte) short | bnp(int,Short) char | bnp(int,Character) int
Integer bnp(Integer,Byte) bnp(Integer,Short) bnp(Integer,Character) Integer
long bnp(long,Byte) bnp(long,Short) bnp(long,Character) bnp(long,Integer)
Long bnp(Long,Byte) bnp(Long,Short) bnp(Long,Character) bnp(Long,Integer)
float bnp(float,Byte) bnp(float,Short) bnp(float,Character) bnp(float,Integer)
Float bnp(Float,Byte) bnp(Float,Short) bnp(Float,Character) bnp(Float,Integer)
double bnp(double,Byte) bnp(double,Short) bnp(double,Character) bnp(double,Integer)
Double bnp(Double,Byte) bnp(Double,Short) bnp(Double,Character) bnp(Double,Integer)
boolean lub(Boolean,Byte) lub(Boolean,Short) lub(Boolean,Character) lub(Boolean,Integer)
Boolean lub(Boolean,Byte) lub(Boolean,Short) lub(Boolean,Character) lub(Boolean,Integer)
null Byte Short Character Integer
Object lub(Object,Byte) lub(Object,Short) lub(Object,Character) lub(Object,Integer)

Table 15.25-D. Conditional expression type (Reference 3rd operand, Part II)

3rd → Long Float Double Boolean
2nd ↓
byte bnp(byte,Long) bnp(byte,Float) bnp(byte,Double) lub(Byte,Boolean)
Byte bnp(Byte,Long) bnp(Byte,Float) bnp(Byte,Double) lub(Byte,Boolean)
short bnp(short,Long) bnp(short,Float) bnp(short,Double) lub(Short,Boolean)
Short bnp(Short,Long) bnp(Short,Float) bnp(Short,Double) lub(Short,Boolean)
char bnp(char,Long) bnp(char,Float) bnp(char,Double) lub(Character,Boolean)
Character bnp(Character,Long) bnp(Character,Float) bnp(Character,Double) lub(Character,Boolean)
int bnp(int,Long) bnp(int,Float) bnp(int,Double) lub(Integer,Boolean)
Integer bnp(Integer,Long) bnp(Integer,Float) bnp(Integer,Double) lub(Integer,Boolean)
long long bnp(long,Float) bnp(long,Double) lub(Long,Boolean)
Long Long bnp(Long,Float) bnp(Long,Double) lub(Long,Boolean)
float bnp(float,Long) float bnp(float,Double) lub(Float,Boolean)
Float bnp(Float,Long) Float bnp(Float,Double) lub(Float,Boolean)
double bnp(double,Long) bnp(double,Float) double lub(Double,Boolean)
Double bnp(Double,Long) bnp(Double,Float) Double lub(Double,Boolean)
boolean lub(Boolean,Long) lub(Boolean,Float) lub(Boolean,Double) boolean
Boolean lub(Boolean,Long) lub(Boolean,Float) lub(Boolean,Double) Boolean
null Long Float Double Boolean
Object lub(Object,Long) lub(Object,Float) lub(Object,Double) lub(Object,Boolean)

Table 15.25-E. Conditional expression type (Reference 3rd operand, Part III)

3rd → null Object
2nd ↓
byte lub(Byte,null) lub(Byte,Object)
Byte Byte lub(Byte,Object)
short lub(Short,null) lub(Short,Object)
Short Short lub(Short,Object)
char lub(Character,null) lub(Character,Object)
Character Character lub(Character,Object)
int lub(Integer,null) lub(Integer,Object)
Integer Integer lub(Integer,Object)
long lub(Long,null) lub(Long,Object)
Long Long lub(Long,Object)
float lub(Float,null) lub(Float,Object)
Float Float lub(Float,Object)
double lub(Double,null) lub(Double,Object)
Double Double lub(Double,Object)
boolean lub(Boolean,null) lub(Boolean,Object)
Boolean Boolean lub(Boolean,Object)
null null lub(null,Object)
Object Object Object

At run time, the first operand expression of the conditional expression is evaluated first. If necessary, unboxing conversion is performed on the result.

The resulting boolean value is then used to choose either the second or the third operand expression:

  • If the value of the first operand is true, then the second operand expression is chosen.
  • If the value of the first operand is false, then the third operand expression is chosen.

The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated below.

This conversion may include boxing or unboxing conversion (§5.1.7, §5.1.8).

The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.

15.25.1. Boolean Conditional Expressions

Boolean conditional expressions are standalone expressions (§15.2).

The type of a boolean conditional expression is determined as follows:

  • If the second and third operands are both of type Boolean, the conditional expression has type Boolean.
  • Otherwise, the conditional expression has type boolean.

15.25.2. Numeric Conditional Expressions

Numeric conditional expressions are standalone expressions (§15.2).

The type of a numeric conditional expression is determined as follows:

  • If the second and third operands have the same type, then that is the type of the conditional expression.

  • If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.

  • If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.

  • If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T.

  • If one of the operands is of type T, where T is Byte, Short, or Character, and the other operand is a constant expression of type int whose value is representable in the type U which is the result of applying unboxing conversion to T, then the type of the conditional expression is U.

  • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

    Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

15.25.3. Reference Conditional Expressions

A reference conditional expression is a poly expression if it appears in an assignment context or an invocation context (§5.2. §5.3). Otherwise, it is a standalone expression.

Where a poly reference conditional expression appears in a context of a particular kind with target type T, its second and third operand expressions similarly appear in a context of the same kind with target type T.

The type of a poly reference conditional expression is the same as its target type.

The type of a standalone reference conditional expression is determined as follows:

  • If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
  • If the type of one of the second and third operands is the null type, and the type of the other operand is a reference type, then the type of the conditional expression is that reference type.
  • Otherwise, the second and third operands are of types S_1 and S_2 respectively. Let T_1 be the type that results from applying boxing conversion to S_1, and let T_2 be the type that results from applying boxing conversion to S_2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T_1, T_2).

Because reference conditional expressions can be poly expressions, they can "pass down" context to their operands. This allows lambda expressions and method reference expressions to appear as operands:

return ... ? (x -> x) : (x -> -x);

It also allows use of extra information to improve type checking of generic method invocations. Prior to Java SE 8, this assignment was well-typed:

List<String> ls = Arrays.asList();

but this was not:

List<String> ls = ... ? Arrays.asList() : Arrays.asList("a","b");

The rules above allow both assignments to be considered well-typed.

Note that a reference conditional expression does not have to contain a poly expression as an operand in order to be a poly expression. It is a poly expression simply by virtue of the context in which it appears. For example, in the following code, the conditional expression is a poly expression, and each operand is considered to be in an assignment context targeting Class<? super Integer>:

Class<? super Integer> choose(boolean b,
                              Class<Integer> c1,
                              Class<Number> c2) {
    return b ? c1 : c2;
}

If the conditional expression was not a poly expression, then a compile-time error would occur, as its type would be lub(Class<Integer>, Class<Number>) = Class<? extends Number> which is incompatible with the return type of choose.