Results 1 to 5 of 5
  1. #1
    skyineyes is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Object class's equals() method behavior????

    Hi

    Can anybody comment on my observation ???

    equal() method of object class will return true IFF

    a) Both references are same?
    b) Will return false even if the data is same but references are different?

  2. #2
    skyineyes is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    The equals() method in the class Object uses only the == operator for comparisons so unless you override equals(), two objects are considered equal only if the two references refer to the same object.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    unless you override equals(), two objects are considered equal only if the two references refer to the same object.
    What do you mean?
    If i override the equals() method, i can only get a "true" if both references are only in the same object?

    If it is, it depends on the new implementation....
    freedom exists in the world of ideas

  4. #4
    M77's Avatar
    M77
    M77 is offline Member
    Join Date
    May 2008
    Location
    LV
    Posts
    43
    Rep Power
    0

    Default

    Equals is ok method. Why it belongs to Object class it's a question. You may need to check if 2 objects equals by content and not by references.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hello skyineyes
    Quote Originally Posted by skyineyes View Post
    Hi

    Can anybody comment on my observation ???

    equal() method of object class will return true IFF

    a) Both references are same?
    b) Will return false even if the data is same but references are different?
    Consider the variables A and B:
    Java Code:
    A.equals(B) is true [COLOR="RoyalBlue"][I]if and only if[/I][/COLOR] A and B references the same object.
    Mostly, we want to see if two objects are equal by value and not by reference. So then, we override the equals() method. In your question, both a and b are true. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. 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
  2. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:10 PM
  3. Replies: 1
    Last Post: 04-17-2008, 11:10 PM
  4. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:22 PM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 AM

Posting Permissions

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