Compareto and equals method

Difference between CompareTo and equals method.

Questions by cspatel

Showing Answers 1 - 9 of 9 Answers

CompareTo() method is derived from Comparable interface and is used to find the Ordering of Strings.
equals() method is from java.lang.Object and used for comparing objects.

  Was this answer useful?  Yes

Bharat

  • Oct 1st, 2012
 

CompareTo method which is present in the String class is used to check the two strings. It checks each character with the other String and if found equals then returns 0. Else negative value or positive value based on which character is higher.

Equals method is used to check the values of the two string are equal then it returns true else false.

  Was this answer useful?  Yes

siva

  • Oct 21st, 2017
 

equals() method is derived from Object class which is a super class in java indirectly and this method is used to verify the content of the two objects if same return true otherwise return false.
In the context of compareTo() method, it internally used some Math calucations to make defference the content of the two strings.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions