Win a copy of Functional Reactive Programming this week in the Other Languages 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