• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null check

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic