• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference variables comparison using equals method

 
vvus bharadwaj
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when we compare reference variables using equals method?(equals method compares what?)
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look this post Equal()
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic