• 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

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

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic