Results 1 to 6 of 6
- 12-28-2010, 12:25 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
equals method to compare two objects?
:confused:
Is it possible to compare 2 objects like we do with Strings using equals() method.
for e.g,
class A
{
int a=10,b=10,c=10;
}
class Equals
{
public static void main(String s[])
{
A a,b;
a=new A();
b=new A();
if(a.equals(b))
System.out.println("We r same...");
else
System.out.println("We r not same...");
}
}
this program gives me "We r not same....."
How does equals() work in this program?
if any one can help.....!!!
- 12-28-2010, 12:51 PM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
if you want to compare objects, use "==" operator
- 12-28-2010, 01:26 PM #3
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
a == b
returns true if and only if a is the same instance as b.
a.equals(b)
returns true if the instances are considered equal by the "equals" method,
if you haven't written any "equals" method it will return a == b.
I suggest you add a equals method to A:
Java Code:@Override //Not needed public boolean equals(A obj) { return (obj != null) && (this.a == obj.a) && (this.b == obj.b) && (this.c == obj.c) }
- 12-29-2010, 06:46 AM #4
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
confusion
thnx,
That means equals method is overridden for all inbuilt classes of Java.
and if we want the same functionality we have to override that method.
Am i right?
- 12-29-2010, 09:41 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 27
- Rep Power
- 0
It is good practice to override the equals method for your own classes and it's a must if there will be checks for equality of instances.
- 12-29-2010, 10:24 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Yep, you're right; as a rule of thumb, if you implement an equals( ... ) method you should also implement the hashCode() method so your class can be stored in a collection (think of a HashSet). Also note that if you want your objects to be 'storable' in such a collection you should be careful when you change the state (member variables) of your object that influence the result of your equals( ... ) method and/or your hashCode() method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Mutable Objects having equals method
By Yadu in forum Advanced JavaReplies: 10Last Post: 08-22-2010, 04:43 PM -
equals method :::::HELP:::::
By alihht in forum New To JavaReplies: 5Last Post: 03-09-2010, 07:19 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
equals method
By mani_miit in forum Advanced JavaReplies: 7Last Post: 09-09-2009, 10:26 PM -
equals method
By timkd127 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks