# What is the use of equals () method?

• 08-04-2011, 11:45 AM
srinivasmallabathula
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
JosAH
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,

Jos
• 08-04-2011, 12:44 PM
Tolls
Quote:

Originally Posted by srinivasmallabathula
What is the use of override equals () method in our program?

<snip code that does not override the equals method>

O/P:
5
5
one and two are not equal
...
what is the reason for this?

Without overriding the equals method supplied by Object the JVM falls back to using that one, which simply checks if two objects are the same object.
• 08-04-2011, 05:57 PM
bnson
Hello, you try to add the code following:

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");         }```
You see the difference between the two class "one" and "two".

What is equlas?
Quote:

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 ==.
Difference between == and equals() ?
Quote:

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.