From the course: Java: Generic Classes

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Understand type erasure in generics

Understand type erasure in generics - Java Tutorial

From the course: Java: Generic Classes

Understand type erasure in generics

- [Instructor] Let us now understand what type erasure is. When you write code with generics. And then when you compile that code the generics information is erased or removed. This process is called type erasure. This essentially means that generics provides type safety checks only at the compile time. And this information is not carried in the bytecode that gets executed at runtime. So what really happens in the type erasure process. In this the Java compiler is going to replace your type parameters. And there are two scenarios here. One is when your generic type has unbounded type parameters. In that case the compiler will re place those type parameters with the Object type. The second scenario is when your generic type has bounded type parameters. In that case, the parameters will be replaced with the corresponding bounds. In addition to this, the compiler also ensures that it applies all the appropriate casts…

Contents