Is java generic code copied at run-time for each type used OR java uses type casting?

Akshay Naik :

I was reading about how monomorphisation works in Rust, does java work in same way.

Lets say I create below file.

public class DummyDTO<T> {
    T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

and use new DummyDTO<Integer>() & new DummyDTO<Float>() will java compiler create two different files for each, Integer and Float or it will Simply replace T with Object and will do casting everywhere?

Elliott Frisch :

Type erasure. The second one. Literally, replace T with Object and it will insert casts as necessary.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27072&siteId=1