• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

comparing objects

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals() method works here when iam creating objects of java's inbuilt class.
public class EqualsMethod {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));
}
result is: true
But when iam creating my own class
class Value {
int i;
}
public class EqualsMethod2 {
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
}
i get false as result.
Please help me.
(Thanks)
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bindesh
The reason the equals method doesn't work for your classes is because the equals method is inherited from the Object class. In the Object class the equals method just does a shallow ( == ) comparison. In some of the Java classes (String, the wrapper classes, and others) equals is overrriden to do a deep compare of the actual objects in question, not just the memory locations like == does.
In order to get equals to work for your class you need to override it to do a comparison on objects of your class in a way that will show if the objects are the same. If i is the only instance variable in your class then override equals to check the values of i for each of the objects being compared then return true of false based on that comparison.
hope that helps
Dave
SCJP

By the way you should re-register following the naming convention here.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Explanation from Dave.
equals() default implementation is just shallow
Wrappers and String class override them
So we get deep comparsion
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dave for your answer.
And also for informing about the naming convention.
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you override equals() then you should also override the hashCode() method of the Object class. The reason is that, if two objects compare equal, they are supposed to have the same hash code.
This rule is not enforced by the compiler.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
I override equals() in my class.But i still can't understand why should one override the hasCode() (as Geff told), my program is working fine without it.And also how to do so.
Any help.
Bindesh Vijayan
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should one override hashCode()I still can't understand.When your programmer works excellent with equals().And if so what will be the code.
Bindesh Vijayan
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic