File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Praveen palukuri
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic