• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding String.equals and equals of Object Class  RSS feed

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could you please tell me the different between String.equals and the equals of Object Class, if both are same then
why we have equals method in String class we can use equals method of object class.


Thanks,
Santhosh Kumar V.K
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they're not the same.

Object.equals:
String.equals:
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any idea yourself why this might be?

Hint: Equals is used for comparing instances of things. Think about what it means for two Object instances to be equal, and what it means for two String instances to be equal.
 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for reply, both are checking the content are equals right then we will go for equals of object class instead of string.equals to check the string object
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh kumar vk wrote:then we will go for equals of object class instead of string.equals to check the string object

I don't understand what you mean by this? You don't get to choose.

But yes, Object has an .equals() method, as does String. The are different for good reason. Pretty much all you need to know about the == operator and .equals() method in relation to Strings can be found in this CodeRanch FAQ article
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!