public class Test3{
public static void main(String[] args)
{
String s="1";
String s1="2";
System.out.println(Compare.compare(s, s1));
}
}
class Compare {
public static Comparable compare ( Comparable o1, Comparable o2 ) {
if (o1.compareTo(o2) > 0)
return o1;
else
return o2;
}
}
The above code output 2, which is correct if String.compareTo(String str)
is used. However the type of o1 is actually raw type Comparable
not Comparable<String>
and even if it is Comparable<String>
, Comparable o2 cannot be passed as parameter.
So, what o1.compareTo(o2)
actually call? and How it actually work