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
posted
0
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'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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.