Me and generics have yet to make peace. For something that *seems* so simple, it has been battle after battle.
I'm trying to make a Comparator to help with a sorting task. Below is a hacked down version of the class which still gives the error.
I'm not understanding why it won't compile. The error message doesn't help, because to me it says "Error ...apple does not equal apple!"
I gave it <T extends String> after having problems with a simple <T>. I figured that it didn't have enough information so I added the constraint. ("extends String" means String or lower in inheritance right?) But still it will not compile if I try to use any String methods on what I believe to be a String. Other attempts just yielded "cannot find symbol" errors. String is in java.lang, so it must be able to find it right? Anyway, any help on this would be great.
TestComparator.java:7: error: incompatible types
T myVariable = o1.toString();
where T is a type-variable:
T extends String declared in class TestComparator
Note that class String is final - it is not possible to subclass it. So declaring a type parameter "T extends String" doesn't really make sense - the only type T that can satisfy the restriction is class String itself. This means that you don't need the type parameter at all, you could just as well just use String instead of the type parameter T.