Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if(null != obj) { ..... }

 
tom chansky
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what the following condition means?

if(null != obj) { ... }

Is the above equivalent to if(obj != null) { ... }?

I found that condition in the description of the org.apache.commons.pool.ObjectPool interface.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It reqiures that you use a full, real (sounding) first and last name for your display name. You can change your display name here. Thanks!

Second: yes, it's the same thing. In the "C" language, writing comparisons in this order makes some sense (for reasons that I won't burden you with right now). In Java, it's unnecessary. Java code like this generally indicates that the author switches back and forth to writing C a lot.
[ February 02, 2005: Message edited by: Ernest Friedman-Hill ]
 
tom chansky
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned that "In the "C" language, writing comparisons in this order makes some sense (for reasons that I won't burden you with right now)..."

OK, why does C prefer/express that condition in that way? Is there any advantage for doing that for a C compiler?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not that C prefers the "constant == variable" style of comparison, it's that that style makes it harder to commit destructive typos there. in C, namely, you can have an assignment inside a comparison statement; you can say things like,

which sets i to 1 and evaluates to true. (i think 1 is true in C; it's been a long while, it might be false.) this can actually be useful, say if you want to set one variable to the value of another and then execute the body of the "if" statement only if that value is 1; that's kindof rare, but does happen. other languages force you to spell out what you're doing when you're doing that - this is intentional.

so, in C, you should always take care to use "==" in comparisons to avoid possibly difficult-to-diagnose bugs. or, alternatively, take care to put the constant part of any comparison that involves a constant first, since even in C, assigning to a constant is a compile-time error. considering possible typos, better to use both precautions.

(you've got to give C this much: at least it isn't FORTRAN. in some early dialects of FORTRAN, you could assign new values to constant literals... which were global, no less! )
[ February 02, 2005: Message edited by: M Beck ]
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the best of my knowledge in C it is better so that the compiler can catch a typing error. such as

if(obj = null)

rather than

if(obj == null)

In Java the compiler will throw a fit because null is not a boolean, but C is less picky about what it takes in a conditional statement. But I don't think that even C lets you assign a value to null.
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Another minor advantage mentionned by thoses who like this style is readability. In the sample below, you may quickly identify cases because the discriminant value is at the beginning of the line. Of course, that's a matter of taste

Best regards,
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lionel Badiou:
Hi all,

Another minor advantage mentionned by thoses who like this style is readability. In the sample below, you may quickly identify cases because the discriminant value is at the beginning of the line. Of course, that's a matter of taste

Best regards,

If XXX, YYY, and ZZZ are integral types, you should prefer a switch statement over the if...else if...else chain anyway.

But that's somewhat off topic...
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Layne]
Well, of course That is actually the purpose of that construct: to look like a switch statement whereas you can't use one

Best regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic