The type of the ternary conditional operator is determined by the types of its 2nd and 3rd operands.
In the case of
input == null ? null : 1
the type is Integer
, which can be assigned both null
and 1
.
The compiler allows your method to return an Integer
since it can be auto-unboxed into an int
, so it fit the int
return type of myMethod
.
The fact that your specific code may throw a NullPointerException
is not something the compiler can detect.