Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    28
    Rep Power
    0

    Default 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?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by srinivasmallabathula View Post
    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?
    You've just answered your own question.
    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.

  4. #4
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Hello, you try to add the code following:

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

    What is equlas?
    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() ?
    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

  1. equals method :::::HELP:::::
    By alihht in forum New To Java
    Replies: 5
    Last Post: 03-09-2010, 07:19 AM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 10:26 PM
  4. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 06:52 PM
  5. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •