What is comparable and comparator interface? List their differences



What is comparable and comparator interface? List their differences..

Answer / Sudhir Singh Chahar

"The Comparable and Comparator interfaces in Java are used for comparing objects. They help define a total ordering between instances of a class.n
1. Comparable Interface: A class implements the Comparable interface to compare its own instances. It defines the compareTo(Object o) method that compares the current object with another object passed as an argument. Each class can implement only one Comparable interface.
2. Comparator Interface: The Comparator interface is used for comparing two objects of any classes. It also defines a compare(T obj1, T obj2) method. Multiple Comparators can be created for a single class and they can be used to define different comparison rules."

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

C and C++ has constructors and distructors, why does Java does not have distructors?

1 Answers   T3 Softwares,


How do you replace a string in java?

1 Answers  


What is a string what operation can be performed out with the help of a string?

1 Answers  


What is general methodology?

1 Answers  


Can we call virtual funciton in a constructor ?

1 Answers   XX,


What is role of void keyword in declaring functions?

1 Answers   Adobe,


What is immutable data?

1 Answers  


What is immutable in java?

1 Answers  


Can a final variable be initialized in constructor?

1 Answers  


What is arrays sort in java?

1 Answers  


List out five keywords related to exception handling ?

1 Answers  


who can we create the object of a class? in how many ways we can create it (max 5)

2 Answers  


Categories