There is no inconsistent type. Your example can be simplified to
public int foo() { return true ? 0 : null; }
Ternar will automatically put 0 in type Integer , which automatically returns back to int . Since a Integer can also be null , both expressions in ternary then have the same type.
In null branches, a NullPointerException will be thrown. See for yourself by changing true to false in my example
In my opinion, this is one of the drawbacks of Java, which comes from the retention of simple old data types, and the rules of type promotion in triple form are especially harmful.
source share