Type erasure and raw types
Generics were introduced in Java version 1.5. To maintain backward compatibility, generics in Java are implemented with a concept known as type erasure. During compile time, in Java all generic type information is lost (erased). So, for example, the Comparable<T> interface gets compiled to Comparable<Object>; there are no generic parameters in the generated Java bytecode. The compiler is responsible for type checks and enforcing type safety. At runtime, you cannot know what the generic parameter was substituted with.
This is one of the reasons why Java has raw types. Raw types are generic types without type arguments. For example, Comparable<T> is a generic interface, and in Java you can declare this type without ...
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