Hi guys, so i was following some examples with generic methods that return a value and i made this:
public class Main { public static void main(String[] args){ System.out.println(max(23,42,1)); System.out.println(); System.out.println(max("alexa","mauricio","scarlet")); } public static <T extends Comparable<T>> T max(T a, T b, T c){ T maximum = a; if(b.compareTo(a)>0) maximum = b; if (c.compareTo(maximum)>0) maximum = c; return maximum; } }
if you compile it, when it compares the strings variables why does it return "scarlet" as greater than the other 2? i seriously don't get it, i tried to look for the compareTo() method but all it mentions is the usage and that it returns a negative number if the variable you are comparing with another is lesser than the other, 0 if they are equal and a possive number if it is greater. But why? can someone please explain how does it compare it? i thought it was because of the length.