Raw type and Wildcard
Raw type
Raw type means the type
is unbounded/unspecified.
The existence of it is for backward compatibility.
But still, it is not recommend to use this raw type.
1 |
|
You will get warning talking about unchecked type.
Raw type doesn’t provide type check, so you can put different type into a single container without getting compilation error.
But this will cause trouble if you try to cast class that is not compatible.
Again, Raw type is not recommended.
Wildcard
Unbounded wildcard
You can’t assign <A>
to <B>
even if the A is inherit from B. This is because of type check in compile time.
In order to assign relevant types to their base type, wildcard
comes into being.
Assignable by any type
Any container can assign to <?>
.
1 |
|
You can even assign raw type container to it.
1 |
|
Type related operations are not compatible
Any operations that type related, including add
, class cast
etc., are not allowed in unbounded wildcard.
1 |
|
Bounded wildcard
For this section, please refer to my POST