Q:

How are ternary expressions in Java evaluated?

I have followed the tutorial provide here and in line:
boolean t1 = false?false:true?false:true?false:true;

the final value of t1 is false. but i evaluated it as true.
first false gives true and that true gives false which further finally gives true 
am i right? no i am wrong .
please tell me how ternary expression are evaluated in java?

A:

When the compiler finds a ? character, it looks for a corresponding :. The expression before the ? is the first operand of the ternary conditional operator, which represents the condition.
The expression between the ? and the : is the second operand of the operator, whose value is returned if the condition is true.
The expression after the : is the third operand of the operator, whose value is returned if the condition is false.
boolean t1 = false   ? false    :    true?false:true?false:true;

             first     second        third
             operand   operand       operand

Since first operand is false, the result is the value of the third operand true?false:true?false:true, so let's evaluate it:
true    ?   false    : true?false:true;

first       second     third
operand     operand    operand

Since first operand is true, the result is the value of the second operand - false.
BTW, the value of the third operand true?false:true is also false, so x?false:true?false:true returns false regardless of the value of x.

A:

We can regroup these operations according to associativity rules.
boolean t1 = (false ? false : (true ? false : (true ? false : true)));
                  |                      |               | 
                  |                      |               1
                  |                      2
                  3

In theory, this is what should happen

Exp 1 is evaluated first and is evaluated as false. 
Exp 2 is now: true? false : false so the final output is false. 
Exp 3 now becomes false ? false : false, which is false.

Now, to get technical, this is what actually happens:

Exp 3 is false ? (don't care) : (...) -> the inner expression containing Exp 2 and Exp 1 need to be evaluated.
Exp 2 is true: false, (don't care) Since we've already determined the value of Exp 2, Exp 1 no longer needs to be evaluated.
Exp 3 is now reevaluated to be false ? (don't care) : false and so the final answer is false.

