12.2 Implementing Generics, Type Erasure, and Raw Types
To understand generics and learn what information is available at runtime, let’s now look at how the compiler translates generics into bytecode.
12.2.1 Implementation Options
In general, two ways to implement generic types exist:
-
Heterogeneous variant: Individual code is generated for each type (such as String, Integer, or Point, which results in three class files). This variant is also called code specialization .
-
Homogeneous translationFrom the parameterized class, a class is created that only uses Object , for example, instead of the type parameter. For the actual type parameter, the compiler places type castings into the statements.
Java uses homogeneous translation, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access