Posts type erasure
Post
Cancel

type erasure

참고:

Type erasure can be explained as the process of enforcing type constraints only at compile time and discarding the element type information at runtime. 타입 제한을 컴파일 타임에만 강제하고, 런타임에는 타입 정보를 제거해버린다.

1
2
3
4
5
6
7
8
public static  <E> boolean containsElement(E [] elements, E element){
    for (E e : elements){
        if(e.equals(element)){
            return true;
        }
    }
    return false;
}

이 코드가 컴파일이 되면 이렇게, unbounded type E를 실제 타입인 Object로 바꾼다.

1
2
3
4
5
6
7
8
9
public static  boolean containsElement(Object [] elements, Object element){
    for (Object e : elements){
        if(e.equals(element)){
            return true;
        }
    }
    return false;
}

type parameter가 bounded인 경우라면

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BoundStack<E extends Comparable<E>> {
    private E[] stackContent;
 
    public BoundStack(int capacity) {
        this.stackContent = (E[]) new Object[capacity];
    }
 
    public void push(E data) {
        // ..
    }
 
    public E pop() {
        // ..
    }
}

first bound class로 치환한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BoundStack {
    private Comparable [] stackContent;
 
    public BoundStack(int capacity) {
        this.stackContent = (Comparable[]) new Object[capacity];
    }
 
    public void push(Comparable data) {
        // ..
    }
 
    public Comparable pop() {
        // ..
    }
}

왜? Java Generic은 JVM 레벨의 호환성을 위해서 컴파일시에 Type 정보를 삭제해버린다. 정말 이 이유가 다일까?

This post is licensed under CC BY 4.0 by the author.