The conditional ? : operator in Java is right-associative.

So the expression

a<b ? c<d ? 1 : 2 : 3 ;

is evaluated as

a<b ? (c<d ? 1 : 2) : 3 ;

Does this mean that the expression (c<d ? 1: 2) is evaluated first or is the boolean a<b evaluated and the expression (c<d ? 1: 2) is only evaluated afterwards iff a<b is true?

In other words: assume c<d is true, is the evaluation then:

  1. a<b ? 1: 3; or
  2. a<b ? (c<d ? 1 : 2) : 3 ;

7 Replies 7

The first thing evaluated is a<b. If (and only if) a<b will c<d be evaluated. If a>=b it will not be evaluated at all and the expression will terminate with 3 being assigned. The ternary operator ? : is only valid for assignment in Java.

If you run https://onlinegdb.com/H4h7KQMcIo which uses A() ? B() ? C() : D() : E() it prints A first and then B and then C

Operator precedence and evaluation order are two different concepts. Try if you can keep them apart. Evaluation order is left to right regardless of precedence.

So the latter:

the boolean a<b (is) evaluated and the expression (c<d ? 1: 2) is only evaluated afterwards iff a<b is true

@EliottFrisch what do you mean that ? : is only valid for assignment? I can pass b ? m : n to a method any time.

And associativity, operator precedence, and evaluation order are three different concepts.

The ternary operator is syntactic sugar and is used to simplify an if/else structure. In this particular case, we have two nested ternary operators, so for analysis purposes, it is best to replace them with that structure:

if( a < b ) {
   return 3;
}
else {
   if( c < d ) {
      return 1;
   }
   else {
      return 2; 
   }
}

Java Language Specification 15.25. Conditional Operator ? : says:

At run time, the first operand expression of the conditional expression is evaluated first. ...

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 operand expression not chosen is not evaluated for that particular evaluation of the conditional expression

That means:

  • the condition is evaluated first,
  • only one of the second or third operand is evaluated

Note: the Conditional Operator (? :,) as its name suggest, is a normal operator, it is not only valid in assignments!
It can be used in any expressions: assignment, array index, method arguments, conditionals [if, for, switch] ... (assignment is also an Expression, = is the Simple Assignment Operator.)

@marce-puente you have to be very careful with replacing the conditional ?: operator with if/else, because it can behave in subtle and potentially surprising ways.

For example:

Number n = true ? Integer.valueOf(0) : Double.valueOf(0);
System.out.println(n instanceof Integer);  // prints false

ideone demo; whereas:

Number n;
if (true) {
  n = Integer.valueOf(0);
} else {
  n = Double.valueOf(0);
}
System.out.println(n instanceof Integer);  // prints true

ideone demo.

(If you've not seen this before, this behavior of the conditional ?: operator is because of binary numeric promotion)

I would suggest that you don't want to focus on the semantics of the conditional ?: operator specifically, but rather remember the much simpler rule:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.