It is said in documentation, that
...wildcard, represents an unknown type. The wildcard can be used ... as the type of a ... local variable
From my point of view, this means I can write something like
int a;
? b;
but I can't.
Is it really possible?
Solved
That's an appallingly worded statement, and I'm surprised to see it in the documentation. You can't use a wildcard as the type of a variable, but you can use a wildcard in the type of a variable.
? a; // no
List b; // yes
This is the documentation for generics. Java is strongly typed, and will not accept a wilcard as a variable type.
Wilcard will only be accepted in generic types like:
List x = new ArrayList<>() or List x = new ArrayList<>()
Nope, not possible. Java is strongly typed, the ? is used in a variety of cases (mostly to do with generics) when you can't know the type ahead of time.
I would suggest you read the rest of the documentation you posted, it has some excellent examples as to how to use ?.
You're confusing what purpose the unbounded wildcard has. In general, it implies that you either do not know or do not care about the type when dealing with various things, such as collections.
For example, a List simpleList is a list that contains an unknown type.
In general, ? extends Object, so if you ever really think you need ?, you'd want to use Object instead.
No it is not possible, the Java Tutorial reefers that you can define a local variable using a wildcard like this:
List localVariable;
but not like this:
? localVariable;
A wildcard is used exclusivity in Java only in Generic brackets, replacing a Type Parameter.