Results 1 to 8 of 8

Thread: equals method

  1. #1
    mani_miit is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default 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.,

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you may also implement Comparable interface and work out method compareTo

  4. #4
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You might want to have a look at this JavaWorld article that discusses overriding nonfinal methods of Object.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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".

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Edit: Nevermind. Gods I'm ashamed. Brain fart!
    Last edited by masijade; 09-09-2009 at 08:23 AM.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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

  1. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM
  2. How to override Equals method with a Java File
    By JordashTalon in forum New To Java
    Replies: 6
    Last Post: 01-22-2009, 04:25 AM
  3. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 06:32 PM
  4. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 12:58 AM
  5. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 05:13 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
  •