File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes if(null != obj) { ..... } Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "if(null != obj) { ..... }" Watch "if(null != obj) { ..... }" New topic
Author

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

tom chansky
Greenhorn

Joined: Nov 01, 2004
Posts: 28
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
tom chansky
Greenhorn

Joined: Nov 01, 2004
Posts: 28
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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Jan 06, 2005
Posts: 140
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,


Lionel Badiou
CodeFutures Software
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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...


Java API Documentation
The Java Tutorial
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
[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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if(null != obj) { ..... }