O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.4. Variables of a Raw Type

You have seen that the run-time type of all instances of a generic type is the same and is just the generic type name without any parameters. You can use the generic type name by itself to define variables. For example:

LinkedList list = null;

This creates a variable with the name list that is of type LinkedList from the LinkedList<T> generic type. This type that results from eliminating the parameters from the generic type is referred to as a raw type.

The class that corresponds to the raw type is produced by removing the type parameters from the generic type definition and replacing each instance of a type variable in the definition by the leftmost bound of its correspond type parameter. This process of mapping from a generic type to a non-generic type is called type erasure because all occurrences of the type variable are effectively erased from the generic class definition. A raw type exists as a consequence of implementing generic types using type erasure.

Since in the absence of any explicit type parameter bounds every type parameter T is implicitly bounded by type Object, all occurrences of T in a generic type definition will be replaced by Object to produce the raw type. This is important for interface types such as Iterable<> and Comparable<> in the standard packages. Interfaces in the standard packages that define methods are generally defined as generic types for maximum flexibility. When you implement such an interface in an ordinary class ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required