Possible Duplicate:
Tricky ternary operator in Java - autoboxing
We know that int roomCode = null;
is not allowed by the compiler.
Then why the Code 1 doesn't give a compiler error, when Code 2 does.
Code 1:
int roomCode = (childCount == 0) ? 100 : null;
Code 2:
int roomCode = 0;
if(childCount == 0) roomCode = 100;
else roomCode = null; // Type mismatch: cannot convert from null to int
null
evaluate to when it takes that path? – NahtanhaIf one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
So in your case it might consider return type of?:
operator as int. – Equivoque