aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer.equals(Long)??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer.equals(Long)???" Watch "Integer.equals(Long)???" New topic
Author

Integer.equals(Long)???

L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37
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.


SCJP SE 6.0, SCWCD J2EE 5
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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


Blog
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

I agree with Vishwa
[ June 19, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1, look at the equals method signature :
boolean equals(Object o);


[My Blog]
All roads lead to JavaRanch
L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37
Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer.equals(Long)???