aspose file tools*
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
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: 19073
    
  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