generic type에 대해서 평소에 아무 생각없이 그냥 사용했는데 오늘은 generic type erasure에 대해서 공부하면서 제네릭에 대해서 확실히 이해해보자
공식 문서에서는 다음과 같이 정의가 되어 있다.
<aside> 💡 Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to:
Replace all type parameters in generic types with their bounds or if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
Object
Insert type casts if necessary to preserve type safety.
Generate bridge methods to preserve polymorphism in extended generic types.
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.
</aside>
출처: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
제네릭은 컴파일 타임에 타이트한 타입 체크를 통해서 제네릭 프로그래밍을 돕기위해서 제공되는 문법이다.
여러 블로그들의 내용을 종합해 정의를 내리면
컴파일 타임에만 타입에 대한 제약조건을 확인 및 검사하고 런타임에는 무슨 타입인지 알 수 없게 하는 것
이라고 대부분 이야기 한다.
지금부터 차근차근 정리를 해보자