File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer.equals(Long)???" Watch "Integer.equals(Long)???" New topic


L. Wei
Ranch Hand

Joined: May 01, 2008
Posts: 37

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

Joined: Jun 04, 2007
Posts: 331

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.


Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

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é

Joined: Nov 24, 2005
Posts: 14688

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:
subject: Integer.equals(Long)???
It's not a secret anymore!