What exactly are wildcards in JAVA generics?

for example, there is such a generic class

public class Gen<T>{
    
}

then there is such a method in other classes

public void showKeyValue(Gen gen){}

public void showKeyValue(Gen<?> gen){}

also, why Gen < Object > and Gen < String > are of the same category (Gen < Object > .Class = Gen < String > .class)
, but Gen < String > can"t point to a reference to Gen < Object > (Gen < Object > gen = new Gen < String > will make an error). Is there any difference between these two methods? if not, what"s the point of this?
A novice, hoping to have a great god to explain

Mar.02,2021

official document contains this problem. Do a porter here, roughly translated as follows:

The

unbounded wildcard (unbounded wildcard) is used in the following two scenarios:

  • if you need to call a method in the Object class, such as toString () .
  • if the code does not depend on a specific type. A typical example is Class : Class < T > class most methods do not need to know the type of T .

for an example of the first case, there are the following ways to print element information in List :

  guidelines for using wildcards in official documents . 

< hr >

I just saw that you have a second question. Gen < Object > gen = new Gen < String > will report an error. This problem is easy to understand: gen.add (new Object ()) this operation is illegal, because the newly added Object instance may not be String instance.

Menu