Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals question

 
Bob Vel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic