I always thought that primitive types in Java cannot be null
, as it is a compile time error if i attempt to do something like this:
int test = null;
However in a ternary operation, it seems to be allowed:
int test = something != 0 ? 5 : null;
Isn't a ternary operation just short for (in this case):
int test;
if (something != 0){
test = 5;
} else {
test = null
}
which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException
due to autoboxing. So why the java-compiler doesn't fetch nonsense like this?