aspose file tools*
The moose likes Performance and the fly likes Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Clarification" Watch "Clarification" New topic
Author

Clarification

Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi all,

plz have a look at the following comparisons.

1) if(a==null)

(or)

2) if(null==a)


Both comparisons are doing the same job.But,is there any difference between these two in terms of efficiency?.If so what is the reason?.plz let me know..

Thanx in advance,

Regards,
Priya.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Not relevant to SCJP, moving to Performance...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No difference.


[Jess in Action][AskingGoodQuestions]
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
Originally posted by Priya Jothi:

plz have a look at the following comparisons.

1) if(a==null)

(or)

2) if(null==a)


Both comparisons are doing the same job.But,is there any difference between these two in terms of efficiency?.If so what is the reason?.plz let me know..

The answer has nothing to do with performance or effeciency.

There is a school of thought that one should be in the habit of always using the latter form because it allows the compiler to catch typos like

if(a = true)

This will *always* return true and the compiler won't complain a bit (the typo is a single "=" instead of "==").

In this school, they point out that had you typed

if(true = a)

the compiler would have complained (you can't set true to anything ).

I personally find that it makes the code hard to read, but that's just me.

Mark
[ July 26, 2005: Message edited by: Mark Wuest ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mark Wuest:

if(a == true)


Of course the best way to write this is

if (a)

the "== true" is totally redundant.

if (a = null)

won't compile in Java. In C(++) it would, and probably be a hard to find bug. That's where the advice originally is coming from.


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
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
Originally posted by Ilja Preuss:
Of course the best way to write this is

if (a)

the "== true" is totally redundant.

Of course, "totally" is in the eye of the beholder. Many folks find it more readable and less cryptic, especially comparing (a == false) to (!a). Many of these same folks also hate that ternary thing.
Originally posted by Ilja Preuss:
if (a = null)

won't compile in Java. In C(++) it would, and probably be a hard to find bug. That's where the advice originally is coming from.

Yes - it does originate in C/C++, though the same bug can still occur in Java not just with booleans. For example, it is common in Java to see if an object reference is null...

Mark
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mark Wuest:
Of course, "totally" is in the eye of the beholder. Many folks find it more readable and less cryptic, especially comparing (a == false) to (!a).


So why not

if (((a == false) == true) != false)

??? Where do you stop?

People who find

if (!a)

too cryptic should perhaps simply give "a" a better name. Is there anything cryptic about

if (!buttonPressed)

???

Yes - it does originate in C/C++, though the same bug can still occur in Java not just with booleans. For example, it is common in Java to see if an object reference is null...


No, that bug cannot occur in Java, because if you use "=" instead of "==" with non-boolean values, it's quite unlikely to compile. If you have some code that does, I'd like to see it...
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
Originally posted by Ilja Preuss:
if (!buttonPressed)

I occasionally just plain do not see that little tiny, itty-bitty ! in there when looking at others' code. It's hard to miss "== false" though. Maybe I need new glasses. Or bigger monitors. I still like "== false" better.

Believe it or not, even when I write C/C++, I use

if(x == 0)

instead of

if(!x)

just because of missing a few of those little buggers.
Originally posted by Ilja Preuss:
No, that bug cannot occur in Java, because if you use "=" instead of "==" with non-boolean values, it's quite unlikely to compile. If you have some code that does, I'd like to see it...

Heh - that's what I get for trying to defend a point I disagree with anyway.

I still remember my first time working on some person's code who did the

if(<some const> == <some variable>)

trick, cursing the person (and the author of whichever book it was he read that told him to do that), and getting sorely tempted to do an emacs exc-x-query-replace-regex to "fix" them all. No, I didn't do it.

Mark
[ July 26, 2005: Message edited by: Mark Wuest ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Mark Wuest:

if (true == a)
I personally find that it makes the code hard to read, but that's just me.


No, not just you, it's me too.


Ilja Preuss
sheriff and author

People who find

if (!a)

too cryptic should perhaps simply give "a" a better name. Is there anything cryptic about

if (!buttonPressed)


People should:
- use a better coding style:

- buy bigger monitors
- increase fontsize
- lern VB (no flamewar to be expected here, isn't it?)

I guess good c/ c++ - compilers should (and do) warn, if they find an assignment inside an condition.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification