Hi,
we use <T> if want to write a generic class like generic Collection classes (introduced as part of
Java 5).
class TestGenerics<T>{
T value;
TestGenerics(T o){
this.value = o;
}
public T getValue(){
return this.value;
}
}
public static void main(String[] args) {
TestGenerics<String> tg = new TestGenerics<String>("Test");
System.out.println(tg.getValue()); // prints Test
}
in above ex. we can pass any Type like '
String', 'Integer' etc.. to the class and same type we can refer while declaring instance variables and as method arguments etc..
where as '?' is allow to asign list of subtypes to super types which is actually not permitted in Generic collections.
like List<Number> lists = new ArrayList<Integer>();//not compile.
but it is psble using '?'
List<? extends Number> lists = new ArrayList<Integer>();
remember, whenever we use '?' in generics means readonly.
--
Thanks.
[ October 02, 2008: Message edited by: Venkata Saraswathi ]