Differences and clarifications of Java generics, objects, and wildcards

I want to understand this concept:

  • T object - shared, will be deleted to the actual type.
  • ? object - will be erased by what?
  • Object an object;

What is the difference between T, ?and Object?

I easily understand # 1, but what about:

Object var;
? var;

What is the difference between the two? I read that I cannot use ?explicitly, for example, Tor any other variable, and that it ?is related to objects, not types.
But what is the practical reason? Why can't I just write Listobjects ( List<Object>) instead of Listwildcards ( List<?>)? Since I do not know the types of objects in both cases.
Also, I would like to know what is erasing for ??

+4
1

T ?:

  • : T , ? .

  • : T . T . , ?, .

  • : T , . ? .
  • : T ( ). ? .
  • : T, new ArrayList<T>(). , ?.
  • : T. ? ( ).

  • : . , . , , : List<String> myList = new ArrayList<String>();, , myList.add("Hello World");, get , , String myString = myList.get(0);, List myList = new ArrayList(); String myString = (String) myList.get(0); (add ).
    , , T String ( ).

    . (?) Object ( ). . , Object. - ? extends Foo, ? Foo ( , Foo (, Foo) ).

? Object T Object .

+9

Source: https://habr.com/ru/post/1625839/


All Articles