반응형
제네릭 메카니즘 “Erasure"
Type Erasure
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 Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
- 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.
Raw Types
A raw type is the name of a generic class or interface without any type arguments. For example, given the generic Box class:
public class Box<T> { public void set(T t) { /* ... */ } // ... }
To create a parameterized type of Box<T>, you supply an actual type argument for the formal type parameter T:
Box<Integer> intBox = new Box<>();
Row Type이 동일한 모든 제네릭 타입은 컴파일된 시점에서 모두가 동일한 타입으로 존재!
Test.java
public class Test<K, V> {
public void f(K k) {
}
public void f(V v) {
}
}
|
localhost:src terrypark$ javac Test.java
Test.java:5: error: name clash: f(V) and f(K) have the same erasure
public void f(V v) {
^
where V,K are type-variables:
V extends Object declared in class Test
K extends Object declared in class Test
1 error
반응형
'Language > Java' 카테고리의 다른 글
이펙티브자바) 규칙 68, 69 (0) | 2015.08.05 |
---|---|
이펙티브 자바) 규칙 59, 60, 61, 62, 63, 64, 65 (0) | 2015.07.30 |
(java) ltrim 구현 (0) | 2015.06.30 |
이펙티브자바 규칙 19) 2015년 6월 21일- 일요일 스터디 카페 방문 (0) | 2015.06.21 |
2015년 06월 17일 이펙티브자바 스터디. (0) | 2015.06.17 |