Organizational Research By

Surprising Reserch Topic

Question:What is the Comparable interface ?



asked Sep 13, 2013 in Java Interview Questions by rajesh
edited Sep 12, 2013
0 votes
20 views



Related Hot Questions

2 Answers

0 votes

 

The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.

The Comparable interface in the generic form is written as follows:

        interface Comparable

where T is the name of the type parameter.

All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of thecompareTo() method is as follows:

      int i = object1.compareTo(object2)
  • If object1 < object2: The value of i returned will be negative.
  • If object1 > object2: The value of i returned will be positive.
  • If object1 = object2: The value of i returned will be zero.


 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.

The Comparable interface in the generic form is written as follows:

        interface Comparable

where T is the name of the type parameter.

All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of thecompareTo() method is as follows:

      int i = object1.compareTo(object2)
  • If object1 < object2: The value of i returned will be negative.
  • If object1 > object2: The value of i returned will be positive.
  • If object1 = object2: The value of i returned will be zero.


 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...