This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

null check  RSS feed

 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the significance of checking null in this way?

if(null == object.a)// where a is class variable of some class.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the significance of checking for null, or why is null first? I have no idea why the code is checking for null,of course; you can't tell the code's intent from one line.

But null is first here because of a C-language programming tradition. In C, the condition of an "if" statement is an int -- C doesn't have a 'boolean' type like Java. 0 is interpreted as false; anything else is true. Furthermore, pretty much anything can be converted to an int without a cast. Therefore, if you want to check if a variable x is equal to 2, and you accidentally write:

if (x = 2) ...

the compiler doesn't warn you of the error (= instead of ==). The value of x is changed, and the expression's value is always true! This leads to lots of sneaky, hard-to-find bugs. So many C programmers train themselves to write

if (2 == x) ...

instead. If you write = instead of == in this expression, the compiler will complain; you can't assign a value to a literal int!

In Java, it's very hard to make the same mistake. The only time you can do the accidental assignment in Java is if x is boolean and you write

if (x = true)

but there's an easy way to avoid that: never compare a boolean to a boolean literal -- it's redundant. Always just write "if (x)" or "if (!x)", and this assignment bug will never happen.

So anyway: the reason this code is written this way is because someone was used to writing this way in C. In Java, it's just ugly, and there's no reason for it.
 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is a general custom in java programming I see checking for any object to null becz a method invocation on a object which is null will throw NullPointer Exception.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I want to know doing is ther any significance in doing null check in reverse way like:
if(null == object.getvalue()) // where getvalue returns some string
but what happens in case if the object itself is null.

I know that if you do it like
if(object.getvalue() == null) //and if the object itself is null it will throw nullpointer exception.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is of absolutely no use to put the null first in this statement. If the object is null a NullpointerException will be given either way.

If you would have a check like this:

if("definitely not null".equals(maybenull)) { ...

this check will prevent a nullpointer if the maybenull variable == null

if(maybenull.equals("definitely not null")) { ...

in the previous line a NullPointerException will be thrown if the object is null

But as I said in the example you gave it is no more than personal preference I guess.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!