hello pals, I am really very confused with the generics type part. When do we use <?>/<?extends Object> <?> always gives an error as it is an unknown type.so I have to use <T> instead of wild card character. K& B also says to use <T> instead of <?>. Then how do i deal with the question using <?>.this part is really tricky.
It depends when and where you are using it. If you a creating a generic method then <T> defines the generic type in that method. If you are defining a generic collection then <?>, <String> is used. I've not touched generics since I took the SCJP so correct me if I am wrong.
Post a code snippet of what you are trying to do. It may help answer you question a bit better.
There is a reason for ? not working in class declaration. The rule is that if you are not going to declare any reference of the type of the generic parameter, then you can use ? instead of T(or infact any identifier can be used instead of T). But if you declare a class as MyClass<?> then what is the use??? You cannot say this
class MyClass<?> { ? obj;//not allowed }
So you must declare it as
class MyClass<A> { A obj;//OK }
but for method parameters and collections, you can use ? if you don't want to create any reference of the type of the parameter or collection.
void hello(List<?> list) { }
you could have also used T instead of ?. But if you want to create a reference of the generic type, you must use an identifier instead of ?
<T> void hello(List<T> list) { T obj = list.get(0); }
you cannot write the above code as
<?> void hello(List<?> list) { ? obj = list.get(0);//? not allowed like this }