This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Long.equals(..)

 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic