3

what is the difference between

<T> void foo(List<T> i) {
.........
}

and

void foo(List<?> i) {
........
}

Both allows only methods of object invoke on i (compiler restriction). Second method with wild card won't let you add any object into the list (we can make it work with a helper method, which is headache). Why do we need unbounded wild card? It is not restricting or relaxing any types to be invoked with. When do we use second approach over first?

Babu Reddy H
  • 186
  • 1
  • 11
  • You can work with T in your methods ,thereby getting compile time safety,like List.add(T).A wildcard will be reduced to java.lang.Object thereby killing any compile time safety as Object may or may not be of the type T – Kumar Abhinav Oct 17 '15 at 15:38
  • indeed they need to have good reasons for introducing wildcard. see section2 of http://homepages.inf.ed.ac.uk/wadler/fool/program/final/14/14_Paper.pdf – ZhongYu Oct 17 '15 at 16:09

0 Answers0