Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Should there be a warning/error here
Should there be a warning/error here [message #1163887] Thu, 31 October 2013 09:33 Go to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
Just stumbled upon this code:

    public boolean test() {
        return Math.random() > 0.5 ? null : true;
    }


There is no warning/error. I guess the compiler will do something like this:

    public boolean test() {
        return Math.random() > 0.5 ? ((Boolean)null).booleanValue() : true;
    }


Is this expected behavior?
Re: Should there be a warning/error here [message #1165213 is a reply to message #1163887] Fri, 01 November 2013 05:56 Go to previous message
Arnav Kumar is currently offline Arnav KumarFriend
Messages: 16
Registered: October 2013
Junior Member
No there is neither error nor warning because

return Math.random() < 0.5 ? null : true;

is returning false hence you have giving it true in your code.
It can be understood like this

if(Math.random() < 0.5)
{
return null;
}
else
{
return true;
}

Here with your code else part is being executed as the condition is not satisfied but if the condition is satisfied the code will give you java.lang.NullPointerException

TechKnow Heights


[Updated on: Fri, 01 November 2013 05:58]

Report message to a moderator

Previous Topic:simple batch program
Next Topic:Batch Sample
Goto Forum:
  


Current Time: Fri Nov 28 17:20:38 GMT 2014

Powered by FUDForum. Page generated in 0.01445 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software