Java Reference
In-Depth Information
ordinary Java types. Each type variable is replaced with its bound, or with
Object
if
it is not bounded.
The virtual machine works with raw types, not with generic classes.
The raw type of a generic type is obtained by erasing the type variables.
The compiler erases the type variables when it compiles generic classes and methods.
For example, the generic class
Pair < T, S >
turns into the following raw
class:
public class Pair
{
public Pair(
Object
firstElement,
Object
secondElement)
{
first = firstElement;
second = secondElement;
}
public
Object
getFirst() { return first; }
public
Object
getSecond() { return second; }
private
Object
first;
private
Object
second;
}
As you can see, the type variables
T
and
S
have been replaced by
Object
. The result
is an ordinary class.
The same process is applied to generic methods. After erasing the type parameter, the
min
method of the preceding section turns into an ordinary method:
public static
Comparable
min(
Comparable
[] a)
{
Comparable
smallest = a[0];
for (int i = 1; i < a.length; i++)
if (a[i].compareTo(smallest) < 0) smallest =
a[i];
return smallest;
}