wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Comparing Integer and Long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing Integer and Long" Watch "Comparing Integer and Long" New topic
Author

Comparing Integer and Long

Boleh Boleh
Greenhorn

Joined: Jul 15, 2009
Posts: 1
Can someone explain to me how logical operators work on Integer and Long objects?


BUT



ALL compile ok, and give the right result. So why is equality "==" incomparable, but >, <, >=, <= are comparable?


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

ALL compile ok, and give the right result. So why is equality "==" incomparable, but >, <, >=, <= are comparable?


When you compare with the == operator, the compiler is assuming that you are trying to compare whether the references are pointing to the same instance. And since there is no way that a Long reference can point to something that is pointed to by an Integer reference, and vice versa, it generates a compile error.

When you compare with the other operators, the compiler is assuming that you are trying to compare the values, as there is no way to compare references in that regard. And the objects are unboxed and then compared.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing Integer and Long
 
Similar Threads
5 Golden Rules of widening, boxing & varargs
About wrap class and ==,equals;
var-args overloading compiler error: ambiguous
Question about wraper classes
Wrappers