• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.equals(Long)???

 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Output: "false"

My questions are:
1, Why can I use equals() to compare Integer and Long? They are different classes. I know equals() can be used between same type of Objects.
2, Why it's false? If we are allowed to compare Integer with Long, the meaningful contents of i and j are 12. Why the return is false?

Thanks a lot.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Objects of two different types can be compared using equals operator. However sun's contract for the equals method states that it should retun false when the object-types differ. Hence the output.

But using == operator to compare two objects that are not in the same inheritance-tree does result in a compiler error.

HTH,
Vishwa
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Vishwa
[ June 19, 2008: Message edited by: Ninad Kulkarni ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1, look at the equals method signature :
boolean equals(Object o);
 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic