This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help needed" Watch "Help needed" New topic
Author

Help needed

Allan Tan
Greenhorn

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
Greenhorn

Joined: Oct 31, 2000
Posts: 7
Thanks a lot!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed