Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Ok, i will try today and let you know :-)

    The class that holds it is person class.
    How do I use the equal method?
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    If I understand your question, then Person class holds all the variables, because they are built in the constructor at the beginning of the class.

    For some reason my code doesn't work... how do I use the equals method, and how is it differrent from what I've done now?

    Thank you very much
  4. #18
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    equals() vs ==


    All classes extend from Object class automatically. Hence, all classes actually has an inherited method equals().

    Many would ask what is the difference between == operator and the equals() method. The == operator only operates on primitive data types; So, when you apply == operator on 2 objects, Java actually compares if both object references are pointing to the same object, instead of making a comparison between the values held by these objects.

    On the other hand, in the equals() method, you may implement your custom comparison semantics. You decide what attributes to be compared against each other and what defines "equality" or "equivalence" for 2 given object instances.

    Take java.lang.String for example, the following code may show you what are the differences between == and equals() method:
    java Code:
     
    public class Test{
          public static void main(String[] args){
               String a = "A";
               String a2 = "A";
               String a3 = a;
               System.out.println("Using == operator, a == a2 yields: "+ (a==a2));
               System.out.println("Using equals(), a.equals(a2) yields: "+ a.equals(a2));
               System.out.println("Using == operator on a and a3 yields :"+ (a==a3));
          }
     
    }


    In the example above, a references the value "A" at a memory location, say, 0000 for instance. a2 references the value "A" too, but at a different memory location, say, 0011 for instance. So, when you apply a == a2, you are actually comparing 0000 against 0011, which would yield false.

    However, if you apply the == operator on a3, you get true as the result, since a3 was being assigned the same reference location as a, so both are now pointing at the same memory location.

    Using equals() however, you would be comparing the value referenced by the location 0000 instead of the reference location 0000 itself, which is "A", against the value referenced by the location 0011, which is also "A", hence the comparison yields true.

    If you download the source code for the String class and have a look at the equals() method, you will see that it is doing what I have explained in the previous paragraph indeed.

    So, in your case, since p.getName() returns a String, which is an object instance, you know what to do next.

    Comments on this post

    • nerazzurri10 agrees : Your brief answer helped!
    Last edited by tvc3mye; January 2nd, 2013 at 06:24 AM. Reason: Refining the code sample
    When the programming world turns decent, the real world will turn upside down.
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    tvc3mye,

    Thank you for this explanation. The code below works:
    Code:
    Person b=new Person (p.getName(), p.getPAddress());
    		if (p.getPAddress().equals(this.pAddress))
    			return true;
    		return false;
    This is highly appreciated!
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Why create a new Person object: b that is not used for anything?
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    Why create a new Person object: b that is not used for anything?
    You are right. Deleted the new object. Thanks.
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    causes any aliasing problem?
    Sorry I don't know what an aliasing problem is in java.
    The extra class object: b appears to be a waste of time.
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    Sorry I don't know what an aliasing problem is in java.
    The extra class object: b appears to be a waste of time.
    You are right, off course

    Thank you for this outline!
  16. #24
  17. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Coding style


    java Code:
     
    if (p.getPAddress().equals(this.pAddress))
    			return true;
    		return false;


    Your codes above can actually be simplified further to a single line:
    java Code:
     
    return (p.getPAddress().equals(this.pAddress));
    When the programming world turns decent, the real world will turn upside down.
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by tvc3mye
    java Code:
     
    if (p.getPAddress().equals(this.pAddress))
    			return true;
    		return false;


    Your codes above can actually be simplified further to a single line:
    java Code:
     
    return (p.getPAddress().equals(this.pAddress));
    I will fix it as you have written, thank you.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo