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 Help needed 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 "Help needed" Watch "Help needed" New topic

Help needed

Allan Tan

Joined: Oct 31, 2000
Posts: 7
Why does the following code prints "Not Equal"?
How does equals perform comparison?
public class Test
public static void main(String args[]) {
Long L = new Long(7);
Integer J = new Integer(7);
if (L.equals(J)) System.out.println("Equal");
else System.out.println(" Not Equal");
nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
(obj1.equals(obj2)) method checks whether the two objects(obj1 and obj2) belongs to the same class.if they belong to the same class they are equal else not.since L and I belong to two different classes(Long and Integer),the answer answer is not equal.hope this helps.
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
This is what the API says for that method:
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object.
This will print out 'true' though:
System.out.println(L.longValue() == J.longValue());
Allan Tan

Joined: Oct 31, 2000
Posts: 7
Thanks a lot!!!
I agree. Here's the link:
subject: Help needed
It's not a secret anymore!