75.0k views
5 votes
What is difference between == , equals() and compareTo() method?


\\ \\ \\
Thanks ​

User Pgngp
by
8.4k points

1 Answer

3 votes

answer

below is explanation from difference between == , equals() and compareTo() method:

== operator: It is a relational operator that compares the reference or memory address of two objects. If both objects point to the same memory location, it returns true; otherwise, it returns false.

equals() method: method of the object class, and it is used to compare the content or value of two objects. The method returns true if the two objects have the same value, and false if they don't.

compareTo() method: method of the comparable interface, and it is used to compare the natural order of two objects. The method returns an integer value that indicates the relationship between the two objects. If the first object is less than the second object, it returns a negative value. If they are equal, it returns zero. If the first object is greater than the second object, it returns a positive value.

User Ravin Singh D
by
8.0k points