.equals question, please read.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: .equals question, please read.

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    .equals question, please read.

    Hi

    Suppose I have a Player class. From that class, I create 2 objects

    Player p1 = new Player("Jen");
    Player p2 = new Player("Jen");

    then why is p1.equals(p2) is evaluated to false?

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    The equals() method is inherited from Object. In object, the equals method is defined to return true if and only if the two objects are the same object. By same object, I mean exists in exactly the same space in memory (so x == y would return true).

    In the case of your example, the objects are not the same. They contain the same information, but they are seperate objects. It's the same as if we have two identicle cars side by side, same make/colour etc. They are still seperate cars.

    If you want to alter the equals method so that it discriminates based on the properties of the object rather than its memory address you have to override the equals method. You could then test that all the properties of one object (such as name, age etc in a person) are the same as in the other object.

    For more information on the equals method see the API for Object which can be found here.

  3. #3
    Join Date
    Jan 2005
    Posts
    45
    Hi

    Do you mean that in my example, the .equals method is comparing the memory addresses of the two objects? Since they are not the same so Java returns false.

    Thanks for your response.

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    Yes, thats exactly what I mean. That is what the default .equals() method in Object does. If you want it to do anything else you have to override it. For example, the String class compares the characters in two strings to see if they're the same. That way to seperate string objects can be declared equal.

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    You can simply override the method in your Player class. That way, you can set it up so that two Player objects are equal if they have the same name or something.

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by scracker
    Hi

    Do you mean that in my example, the .equals method is comparing the memory addresses of the two objects? Since they are not the same so Java returns false.

    Thanks for your response.
    yes, because java isnt artificially intelligent enough to know that two objects that just happen to hold the same string, are equal.. so all it goes off is the memory location..

    whether or not they are infact equal, is a choice you have to make. you make it by rewriting .equals to compare the things youre interested in.. i.e. player names
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center