8/3/2011

I Just Found Java Compiler Bug

Filed under: — By Aviran Mordo @ 9:18 am

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.

 

2 Responses to “I Just Found Java Compiler Bug”

  1. wp review site nulled Says:

    Cialdini Shares For Creating Traffic From Twitter (or wherever) http://ow.ly/69GLq?e=328l27

  2. Lattie Says:

    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! ;)

Leave a Reply

You must have Javascript enabled in order to submit comments.

All fields are optional (except comment).
Some comments may be held for moderation (depends on spam filter) and not show up immediately.
Links will automatically get rel="nofollow" attribute to deter spammers.

Powered by WordPress