aspose file tools*
The moose likes Java in General and the fly likes Which is the more correct way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which is the more correct way?" Watch "Which is the more correct way?" New topic
Author

Which is the more correct way?

Sivaraman Lakshmanan
Ranch Hand

Joined: Aug 02, 2003
Posts: 231
Hi,
I could see many programmers code in different way they check for null conditions. The 2 common ways of checking are

and other way is


Which is the correct ways of checking. Can some please clarify this.

Regards,
Sivaraman.L
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
I think it's down to personal preference - they will both work equally well.

Steve
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
The difference is in whether you want to catch that you have mistakenly typed "=" instead of "==".

if (null = varName)

doesn't compile, while

if (varName = null)

does.


Ping & DNS - my free Android networking tools app
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ulf Dittmer:
The difference is in whether you want to catch that you have mistakenly typed "=" instead of "==".

if (null = varName)

doesn't compile, while

if (varName = null)

does.


That's true for C++, but not for Java. "null" is not a boolean expression in Java. Try it!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ok, then, it's a habit consistent with coding

1 == var

instead of

var == 1

It's also consistent with

"literal".equals(var)

which avoids a test for (var != null) first.

I put literals and constants on the left almost all the time. Looks a little odd at first, but keeps me out of trouble now & then.
[ August 04, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Stan James:

I put literals and constants on the left almost all the time. Looks a little odd at first, but keeps me out of trouble now & then.

[ August 04, 2005: Message edited by: Stan James ]


and can be avoided, if your programming environment is able to warn on probably unmeant assignments (like eclipse).


http://home.arcor.de/hirnstrom/bewerbung
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The correct way is not to use Java. The very existence of null itself is a language flaw. You can workaround its existence optimally by failing as early as possible upon its detection i.e. entry into contract implementations (concrete methods). This optimal approach works surprisingly well, but is only optimal within the context of Java. The correct approach, and moreso for a statically typed language, would have failure at compile-time (no such thing as null).


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is the more correct way?