Currently am working on an assignment that puts the concepts of Comparable
to application.
I wrote this simple method that allows the input of a Comparable[]
array. This method returns the minimum value of any given array input. Using the .compareTo()
method, I am able to really extend this method to allow object types of really, anything, whether it's custom implementation like a Point
class (which we did for the assignment, but not shown here).
public static Comparable getMinimum(Comparable[] inputArray)
{
Comparable newObj = inputArray[0];
for(int i = 0; i < inputArray.length; i++)
{
Integer retValue = (inputArray[i]).compareTo(newObj);
if(retValue < 0)
{
newObj = inputArray[i];
}
}
return newObj;
}
My question is - what is this Comparable
type? Looking at the Java API, it didn't really reveal much. Why are we using the Comparable
type as opposed to the Object
type, which also includes types like int
, double
, String
, etc?