my dog learned polymorphism*
The moose likes Beginning Java and the fly likes what is the difference between a==10 and 10==a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the difference between a==10 and 10==a" Watch "what is the difference between a==10 and 10==a" New topic
Author

what is the difference between a==10 and 10==a

Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
hi,
i checked above two conditions both are giving same result.i think there is some performance effect by using 10==a. if any one knows pls reply me

Thanks in advance.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

i think there is some performance effect by using 10==a



Try the program and check if there is any difference..

For me output was:

One 1130395840390 Two 1130395840390
true
One 1130395840390 Two 1130395840390
Diff 0

the values current time changes but others remain same even if you execute it many times..
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
No performance differenced you'd ever need to worry about.

You do see people write == tests "backwards" for a good reason. You could easily mistype "a = 10" instead of "a == 10". It's likely to be valid syntax so the compiler won't catch it, but you sure won't get the results you want. If you turn around "10 = a" the compiler will complain and help catch your tiny typo.

And when you get to objects, the "backwards" syntax helps avoid null pointer exceptions. Consider these two lines

If input is null, the first line throws an exception, but the second doesn't. For some reason it bugs me to see this:

Oh, one reason is it's easy to again mistype & instead of && and still get the darned exception!
[ October 27, 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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
You do see people write == tests "backwards" for a good reason. You could easily mistype "a = 10" instead of "a == 10". It's likely to be valid syntax so the compiler won't catch it, but you sure won't get the results you want.


It's likely in C/C++, but not in Java.


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
Yeah, I guess it would only work on booleans?

if ( x = true )

which would be silly in the first place. Still, it seems like I got burned on this at some point ... the syntax for = and == both worked. Can't remember what I was doing tho.

I almost added to that post that the "backwards" syntax very often tells you the author wrote C before Java. I didn't but I still write this way some times.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
Yeah, I guess it would only work on booleans?

if ( x = true )

which would be silly in the first place.


Exactly!

I'm not sure, but I seem to remember that some compilers (Eclipse?) can flag a warning for such code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference between a==10 and 10==a