Results 1 to 8 of 8
Thread: equals method
- 09-07-2009, 09:35 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
equals method
can any one tell me.,
1 class equ{
2 String title;
3 int age;
4 equ(String title,int age){
5 this.title=title;
6 this.age=age;
7 }
8 public static void main(String[] a){
9 equ e=new equ("one",12);
10 equ e1=new equ("one",12);
11 System.out.println(e.equals(e1));
12 String s=new String("hello");
13 String s1=new String("hello");
14 System.out.println(s.equals(s1));
}
}
o/p of line 11 is false,and line 14 true
i don't know how it's compare
plz answer me.,
- 09-07-2009, 09:39 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because e and e1 are separate objects and you have not defined an equals method. So, the equals method of Object is used, which compares reference values, not contents, and so, since e and e1 are separate instances, they have separate reference values and so are unequal. Define an equals method in your class, that overrides Object's equals method, in order to compare the contents of those instances rather than their reference values.
- 09-07-2009, 12:06 PM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you may also implement Comparable interface and work out method compareTo
- 09-07-2009, 01:59 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 26
- Rep Power
- 0
this is because e.equals(e1) compare the two instance of class which donot refer to same object so it is returning false, as if u want to compare the values then go for e.title.equals(e1.title) ------------->this will return true.
- 09-08-2009, 01:27 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You might want to have a look at this JavaWorld article that discusses overriding nonfinal methods of Object.
- 09-09-2009, 04:26 AM #6
Just to clarify a bit:
Object.equals() essentially does
this == otherObject
which checks to see if they are the same instance.
You can override equals() to provide enhanced behavior. Make sure you test for otherObject == null and that otherObject is a subclass of the class you are overriding (use the instanceof operator).
If you override equals(), make sure you override hashcode() as well, so that it returns the same value if two instance are "equal".
- 09-09-2009, 07:20 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Edit: Nevermind. Gods I'm ashamed. Brain fart!
Last edited by masijade; 09-09-2009 at 07:23 AM.
- 09-09-2009, 10:26 PM #8
Masijade made a good point before he erased it, although we actually agreed.
A hash code is used for two purposes.
1. It is used as a simplified means of checking for equality. If the two hash codes are NOT equal, then the objects are not equal.
2. It is used to take a possibly orderly list and scramble it. Certain types of lists rely on the list entries being evenly scattered to work efficiently.
Putting these things together, hash codes can make things run much faster.
The catch is that two objects can have the same hash code but NOT be equal. A good hash algorithm will make this very infrequent, but, if the hash codes are the same, equals() must be invoked to determine for sure that the objects are equal.
Most of the time, the objects are not equal, so the hash code speeds things up.
Similar Threads
-
equals method
By timkd127 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:52 PM -
How to override Equals method with a Java File
By JordashTalon in forum New To JavaReplies: 6Last Post: 01-22-2009, 03:25 AM -
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09-21-2008, 05:32 PM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM -
Why Equals method should be over ridden in Hashcode?
By skyineyes in forum New To JavaReplies: 1Last Post: 05-26-2008, 04:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks