This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variables comparison using equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variables comparison using equals method" Watch "Reference variables comparison using equals method" New topic
Author

Reference variables comparison using equals method

vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
What happens when we compare reference variables using equals method?(equals method compares what?)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

look this post Equal()


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

if the objects you are comparing have overridden equals method( from java.lang.Object class) then it depends upon your implementation. as to how you implements equals method for establishing the equality of two objects. say you have a Dog class with instance field as String breed, with getter for breed field. you can writer your equals method as follows : here we are establishing how 2 dog objects can be equal. we used breed instance field. you can use your own implementation. inside equals method , i call equals method on Sting. Since string class has already overridden equals method it can compare 2 strings and can return true, or false depending upon the literal string contained in the two string objects. also , if you don't override equals method then inherited equals method from the Object will run, which uses == operator for comparisons, which further means two references are only equal if they refer to same object.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reference variables comparison using equals method
 
Similar Threads
equals doubt??
Hash Code and Equals()
equals and == Confusion when using with String, StringBuffer
What is the difference between operator == and method equals()?
Question about in.next() for a database.