본문 바로가기
Java 배우기

Erasure of Generic Types 제네릭 타입의 이레이저

by 노화방지 Anti-aging Hairstyle 2016. 1. 24.
반응형


제네릭 타입의 이레이저 Erasure of Generic Types


타입 이레이저 프로세스 동안에, 자바 컴파일러는 모든 타입 패러미터들을 지우고, 타입 parameter가 bound 되어 있다면 각각을 그 첫 번째 bound로 대체하고, 타입 parameter가 unbound되어 있다면 Object로 대체합니다.

singly linked list에 있는 노드를 표현하는 다음 제네릭 클래스를 검토하십시오:

During the type erasure process, the Java compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded, or Object if the type parameter is unbounded.

Consider the following generic class that represents a node in a singly linked list:

public class Node<T> {

    private T data;
    private Node<T> next;

    public Node(T data, Node<T> next) }
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}

Because the type parameter T is unbounded, the Java compiler replaces it with Object:

public class Node {

    private Object data;
    private Node next;

    public Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Object getData() { return data; }
    // ...
}

In the following example, the generic Node class uses a bounded type parameter:

public class Node<T extends Comparable<T>> {

    private T data;
    private Node<T> next;

    public Node(T data, Node<T> next) {
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}

The Java compiler replaces the bounded type parameter T with the first bound class, Comparable:

public class Node {

    private Comparable data;
    private Node next;

    public Node(Comparable data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Comparable getData() { return data; }
    // ...
} 


반응형

댓글