I am Currently learning Generics I have this Generic class here
package generics.lesson;
public class Pair<T> {
private T first;
private T second;
public Pair(){ first = null; second = null; }
public Pair(T first, T second){ this.first = first ; this.second = second;}
public T getFirst() { return first; }
public T getSecond() { return second; }
public void setFirst(T first) { this.first = first; }
public void setSecond(T second) { this.second = second; }
}
So far I understand this but then I came across this
public static Pair<String> minmax(String[] a)
{
if(a.length == 0 || a == null) return null;
String min = a[0];
String max = a[0];
for(int i = 1 ; i < a.length ; i++)
{
if(min.compareTo(a[i])>0) min = a[i];
if(max.compareTo(a[i])<0) max = a[i];
}
return new Pair<String>(min,max);
}
What confuses me is this
public static Pair<String> minmax(String[] a)
QUestion
How is this possible? and can a generic class be a method return type?
If this works
(which you said you “understand this”)
I would expect this to also work
BTW: In Java 1.4 neither works, In Java 5.0+ both work. 😉