Results 1 to 4 of 4
- 08-04-2011, 11:45 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 25
- Rep Power
- 0
What is the use of equals () method?
What is the use of override equals () method in our program?
I know it compares content of objects.But the following program gives output in different way?
public class EqualsTest {
public static void main (String [] args) {
Moof one = new Moof(5);
Moof two = new Moof(5);
System.out.println(one.getMoofValue());
System.out.println(two.getMoofValue());
if (one.equals(two)) {
System.out.println("one and two are equal");
}
else{
System.out.println("one and two are not equal");
}
}
}
class Moof {
private int moofValue;
public Moof(int val) {
moofValue = val;
}
public int getMoofValue() {
return moofValue;
}
}
O/P:
5
5
one and two are not equal
Why it gives this? according to my knowledge it has to give
5
5
one and two are equal
what is the reason for this?
- 08-04-2011, 12:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
Imagine I have two boxes, a large one and a small one; both contain the number 5; are those boxes equal? Do they contain the same content? This is what the equals( ... ) method is for.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-04-2011, 12:44 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 08-04-2011, 05:57 PM #4
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Hello, you try to add the code following:
You see the difference between the two class "one" and "two".Java Code:if (one.equals(two)) { System.out.println("one and two are equal"); } else { System.out.println("One: " + one + " -- two: " + two); System.out.println("one and two are not equal"); }
What is equlas?
Difference between == and equals() ?Compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.
The == operator is a simple comparison of values. For object references, the values are the references, so x == y returns true if x and y reference the same object.
The equals method can compare two different objects for equality. But here, "equal" is defined by the programmer when they override the method for a specific class.
For example, if you are writing a Car class and all you care about is color, then you could implement equals to return true if both cars are the same color. It's probably not the best way to compare cars, but the point is it's up to the programmer to decide what "equals" means.
Similar Threads
-
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 -
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09-21-2008, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks