aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals question 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 "equals question" Watch "equals question" New topic
Author

equals question

Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
public class EqualsClass1 {
public static void main(String[] args) {
Integer i1 = new Integer(50);
Integer i2 = new Integer(50);
System.out.println(i1.equals(i2));
}
}
Result is true - Perfect

class Value {
int i;
}
public class EqualsClass2 {
public static void main(String[] args) {
Value b1 = new Value();
Value b2 = new Value();
b1.i = b2.i = 75;
System.out.println(b1.equals(b2));
}
}
Result is false - How come?
Please explain. Thanks in advance.

Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Bob,
The wrapper classes (Boolean, Byte, Short, Integr, etc) override Object.equals() to return true if (a)the objects are of the same type and (b)they contain the same values. Which is why your first example returns 'true'. i1 and i1 are of the same type, Integer, and contain the same value, 50.
In the second example, your class Value does not override Object.equals(). The default behaviour provided in Object.equals() only returns true if both reference variables point to the same object in memory i.e. for b1.equals(b2) to be true, b1 == b2 must be true. In your example they are not. b1 and b2 point to two different Value objects in memory.
For more info, check the API for the Object.equals() method and the equals() methods in the Integer class.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Jane's explanation is very sufficient.
I just wanted to add 2 cents to it
Basically every other class extends Object so they all get
equals() by default. However default equals() are shallow (ie
== is what happenin) .
To make them deep copying you need to override it
and thats what happenin in Wrapper and String classes
Cheers
Harsha Jay
Ranch Hand

Joined: Jul 23, 2001
Posts: 177
I am bit confused .... then suppose variable i in class value is now declared in EqualsClass2 and same process is followed
Would the result change???
Pls Explain
Thanks
Harsha Jay
Ranch Hand

Joined: Jul 23, 2001
Posts: 177
What i meant was this
public class EqualsClass2 {
int i;
public static void main(String[] args) {
EqualsClass2 b1 = new EqualsClass2();
EqualsClass2 b2 = new EqualsClass2();
b1.i = b2.i = 75;
System.out.println(b1.equals(b2));
}
}

Tks
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Here issue is the same, every class implicitly inherits equals() method, which if not overrided does not perform deep comparison, so the reult will be false.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The default equals() method only compares if two pointers are pointing to the same object. It does not compare the values of anything inside the object.
In order for your example to work, you must supply an:
equals(Object o) method.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
You need to override the equals method in the Value class to get the kind of result you expect as with the Integer.
For all the wrapper classes, the equals method has been overriden to compare the contents of the value it holds.
HTH,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals question