For example:
int anInt = null;
fails at compile time but
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("" + getSomeVal());
}
}
public static int getSomeVal() {
return new Random().nextBoolean() ? 1 : null;
}
fails (usually) at run time. Trying to return just null
will also result in a compile error, so I assume there is something about having multiple paths that causes the compiler to infer that null
is potentially an autoboxed int
? Why can javac not fail to compile both cases with the same error?