Java Reference
In-Depth Information
}
Now the leftmost bound for the type parameter is type Object , so the raw type is produced by replacing
the type variable T by Object in the generic type definition.
NOTE Youcanstoreareferenceofanyofthetypesproducedfromagenerictypeinavariable
of the corresponding raw type. For example, you could write:
LinkedList list = new LinkedList<String>();
This is legal for compatibility with code written before generic types were available in Java.
However, you should not regard it as part of your normal programming repertoire as it ' s an
inherently risky practice.
WILDCARDS AS TYPE PARAMETER
ARGUMENTS
You express a particular type from the set defined by a generic type by supplying a type argument for each
of its type parameters. For example, to specify the BinaryTree<T> type that stores objects of type String ,
you specify the type argument as String — so the type is BinaryTree<String> . Instead of supplying a
specific type as the type argument for a generic type, you can specify the argument as ? , in which case you
have specified the type argument as a wildcard . A wildcard type represents any class or interface type.
You can declare variables of a generic type using a wildcard type argument. For example:
BinaryTree<?> tree = new BinaryTree<Double>();
The tree variable is of type BinaryTree<?> so you can store a reference to any type of BinaryTree<>
object in it. In this instance you have stored a reference to an object of type BinaryTree<Double> , but Bin-
aryTree<String> or BinaryTree<AnyType> is equally acceptable — as long as the type argument is not
a primitive type. You can think of the use of a variable of a wildcard type as loosely paralleling the use of
a variable of type Object . Because tree is the result of a wildcard type argument, the actual type of the
reference stored is not known, so you cannot use this variable to call methods specific to the object that it
references.
So what is the difference between using a wildcard type argument to a generic type and using a raw type
that you are supposed to avoid? The compiler applies much stricter rules to the wildcard parameterized type
than to the corresponding raw type. Operations performed on the raw type that result in “unchecked" warn-
ings from the compiler are rejected as errors so the code won't compile. Consequently, using a wildcard type
parameter is much safer than using a raw type.
You can use a wildcard type argument to specify a method parameter type where there is no dependency
in the code on the actual type argument for the generic type. If you specify the type of a parameter to
a method as BinaryTree<?> then the method accepts an argument of type BinaryTree<String> , Bin-
Search WWH ::




Custom Search