• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

equals question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic