I Just Found Java Compiler Bug
I just found a bug in Java compiler (java version “1.6.0_24″), where the compiler allows to return null in a method signature that return int.
Here is an example of the code that should not compile.
public int foo()
{
return true ? null : 0;
}
Of course the “true” can be replaced with any boolean expression.
The java compiler compiles this code without problem, however at runtime you’ll get NullPointerException.
Tweet
|

RSS Feeds 

August 23rd, 2011 at 3:38 am
Cialdini Shares For Creating Traffic From Twitter (or wherever) http://ow.ly/69GLq?e=328l27
October 8th, 2011 at 12:48 pm
The behavior is correct since it is legal to return an Integer object when an ‘int’ is expected.
null references the ‘NULL’ integer object.
That all being said: Don’t write code like that!