Skip to content
Commitwise
Menu

Type erasure

Removing generic type arguments after compilation, so List<String> and List<Int> are the same class at runtime.

Definition

Java and Kotlin check generic types at compile time and then erase the type arguments in the compiled code. At runtime, code can't ask whether a list is a List<String>, can't create a T, and libraries need extra help (TypeToken, TypeReference, reified type parameters) to know the full type. Swift keeps generic type arguments at runtime instead.

In practice

new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass() is true in Java.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.