what is meant by "some unknown type"
Exactly what it means - the Set
has some generic parameter, but we don't know what it is.
So the set assigned to a Set<?>
variable might be a Set<String>
, or a Set<Integer>
, or a Set<Map<Integer, Employee>>
or a set containing any other specific type.
So what does that mean for how you can use it? Well, anything you get out of it will be an instance of the ?
, whatever that is. Since we don't know what the type parameter is, you can't say anything more specific than that elements of the set will be assignable to Object
(only because all classes extend from it).
And if you're thinking of adding something to the set - well, the add
method takes a ?
(which makes sense, since this is the type of objects within the set). But if you try to add any specific object, how can you be sure this is type-safe? You can't - if you're inserting a String, you might be putting it into a Set<Integer>
for example, which would break the type-safety you get from generics. So while you don't know the type of the generic parameter, you can't supply any arguments of this type (with the single exception of null
, as that's an "instance" of any type).
As with most generics-related answers, this has focused on collections because they're easier to comprehend instinctively. However the arguments apply to any class that takes generic parameters - if it's declared with the unbounded wildcard parameter ?
, you can't supply any arguments to it, and any values you receive of that type will only be assignable to Object
.