I am studying now Reflection. In the example there is a string

Class<?> cls1 = Integer.class; 

What does the question mark mean?
And why not just write

 Class cls1 = Integer.class; 
  • five
    he means any class - Grundy
  • And what is the meaning of it in this context? Class is already as I understand the type of the variable cls1. Why is a generic here? - Alexander Tishchenko
  • Without <?> Is an outdated syntax and just a bad form. - Pavel Mayorov

1 answer 1

Grundy is right.
In theory, it can be useful in three cases.

  1. When there are several generalization classes and only one must be left unknown.
  2. When you need the same variable to initialize objects with different types of generalization. If you use Object then when initializing the new List<Integer> will be an error, and with <?> You can initialize this variable both as new List<Integer> and as new List<String> .
  3. Based on the previous one, you can define it by some interface or superclass <? super Object> <? super Object> , <? extends Numeric> <? extends Numeric> and initialize with any class that fits these criteria, and not just specifically with this interface or class (A sort of double filtering).