Answer the question
In order to leave comments, you need to log in
Why is type information retained despite type erasure?
class Generic<T> {
T obj;
void set(T obj) { this.obj = obj; }
T get() { return obj; }
}
public class Program {
static public void main(String[] args) {
Generic<String> g = new Generic<>();
Generic<Integer> f = new Generic<>();
g.set("String");
f.set(123);
System.out.println(g.get() + f.get());
}
}
Answer the question
In order to leave comments, you need to log in
At first, it may seem that the JVM creates a new version of the class where and replaces T with the desired type. But in fact, the JVM partially 'tweaks' an already existing parameterized class. Including replaces T with the version of the desired value each time it is required. That is, there is one class, not several versions with fields of different types.
In your case, first the JVM corrects T obj to Object obj (which is downcast converted to String) and then again corrects String obj to Object obj (which is then also converted to Integer implicitly)
g.set("String"); f.set(123);
T obj is actually replaced with Object obj . This process is called erasure, which occurs at the compilation stage. And then there is an implicit downcast.class Test<T> { T val; // на самом деле T заменяется на Object Test(T o) { val = o; } T get() { return val; } } class SimpleClass { void info() { System.out.println("я метод info() из класса SimpleClass"); } } Test<SimpleClass> v = new Test<>(new SimpleClass()); SimpleClass v2 = v.get(); // при инициализации v2 на самом деле происходит неявное нисходящее преобразование к нужному типу //что происходит на самом деле: //SimpleClass v2 = (SimpleClass) v.get(); v2.info();
Erasure of generalized types occurs at compile time. In the simplest case, an unrestricted type is erased to Object, the generic class NameClass to NameClass.
In other words, information about generics exists only at compile time and is not available at runtime.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question