I would like to learn about usage? in java generics. Having examined the T placeholder and the template ?, I wondered ?, I went through several websites / pages and books, but did not understand this. So I created the class below to explore the differences.
import java.util.List; public class Generics2 { public <T> void method1(List<T> list){ System.out.println(list); } public <T extends Number> void method2(List<T> list){ System.out.println(list); } /*public <T super Integer> void method3(List<T> list){ }*///super does not work. public void method4(List<?> list){ System.out.println(list); } public void method5(List<? extends Number> list){ System.out.println(list); } public void method6(List<? super Integer> list){ System.out.println(list); } public <T> void copy1(List<T> list1, List<T> list2){ list1,List<?> list2){
Here in one case there may be several scripts that are not in my class, therefore, what I wrote is incomplete, in this case, someone can help me implement more scripts. Or did I find? as redundant, in addition to providing functions such as using the super keyword and the smaller characters in the method signature along with the return type.
EDIT: Basically, my question is to know the reasons associated with the implementation? wildcard where a generic type can replace it everywhere. It's not about how to use? or T. Of course, knowing that using it will provide some users. For example, the things that I deduced:
- ? makes the code more readable and less easy to code to specific places
- Sometimes this sometimes leads to bloat code.
- We can use superclasses, which is not possible when using type T.
- Limits the addition of new random items to the list. If casting (without classcastexception) is valid for type T sometimes.
Is there any more?
source share