File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Long.equals(..) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Long.equals(..)" Watch "Long.equals(..)" New topic
Author

Long.equals(..)

Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Can anyone suggest a good reason why ...

(new Long(5)).equals(5)

... evaluates to false in my JVM?

I found that ...

(new Long(5)).equals(new Long(5))

... evaluates to true.

What's the difference between the two statements?


Fun programming etcetera!
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Doh! Never mind.

Because equals() accepts an Object parameter, the JVM doesn't promote the 5 literal to a long before auto-boxing. The object passed to equals() method is of type Integer.
 
Don't get me started about those stupid light bulbs.
 
subject: Long.equals(..)
 
Similar Threads
boolean comparision
hashcodes
regarding scjp 1.5
Doubt in StirngBuffer
Differnce between == and equals