为什么类型擦除后信息被保存了?

Why was the information saved after the type erasure?

class Generic<T> {
    void method(T t) {
        print(t.getClass().getSimpleName());
    }
}
public class Program {
    static public void main(String[] args) {
        Generic<String> g = new Generic1<>();
        g.method(new String());
    }
}

正如预期的那样,输出是 String。为什么发生了类型擦除,类型信息就保存下来了?如果我理解正确的话,输出应该是Object

出于与此相同的原因:

            Object s = new String();
// Note ----^^^^^^
            System.out.println(s.getClass().getSimpleName());

对象仍然知道它是什么,并且可以在我们通过getClass询问它时告诉我们。

这与泛型无关。

每个对象knows it class object。您得到 "String" 作为输出,因为这只是字符串对象(顺便说一下,您正在创建)的运行时 class。