Type Erasure in Java…

A way to ease your RAM

What is Type Erasure?

The process of imposing type constraints at compile-time and discard the element type information at runtime.

Type Erasure Pros.

  • Backward compatibility in Two-way
  • Much easier to implement
  • No byte code or Virtual Machine needs to be changed
  • JIT compiler spent less time and less RAM is used to store the generated code due to generating from one version for all generic instances.
  • Better in Performance due to smaller number of objects in the heap memory.

Types of type erasure.

  • Class level Type erasure
  • Method level type erasure

Class-type erasure?

Here the compiler ignores the type parameters on the class and replaces them with the first bound.

raw implementations
after compiler replacements
bounded type parameter
after compiler replacements

Method-type erasure?

For easy understanding, method-level erasure can be stated as follows.

  • If bound, the method’s parameter is converted to its first bound class.
method declaration on two viewpoints
method declaration on two viewpoints

Edge cases?

In the process of type erasure compiler sometimes create synthetic methods when there are similar methods. in fact, this is mainly because to distinguish one method from the other.

Bridge methods.

The ambiguity of method signatures in extended parameterized class or implemented parameterized interface by a class or interface create these Bridge methods. In fact, these are synthetic methods to solve the edge case that we discussed in the above section and also for covariant return types.

Compiler Bridge methods

Tech enthusiast…</>

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store