Erasure and the Upper Bound of a Type Parameter

When the compiler translates method maximum into bytecodes, it uses erasure to replace the type parameters with actual types. In Fig. 20.3, all generic types were replaced with type Object. Actually, all type parameters are replaced with the upper bound of the type parameter, which is specified in the type-parameter section. Figure 20.6 simulates the erasure of method maximum’s types by showing the method’s source code after the type-parameter section is removed and type parameter T is replaced with the upper bound, Comparable, throughout the method declaration. The erasure of Comparable<T> is simply Comparable.

 1   public static Comparable maximum(Comparable x, ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.