简体   繁体   English

编译时引用类型相等

[英]Reference Type Equality at compile time

I came across following statement in Java Language Specification-8.It states that 2 classes are same at compile time if:我在 Java Language Specification-8 中遇到了以下语句。它指出,如果满足以下条件,则 2 个类在编译时是相同的:

  1. They have same binary name(Canonical Name for outer classes).它们具有相同的二进制名称(外部类的规范名称)。
  2. Their type arguments are same (if any).它们的类型参数是相同的(如果有的话)。

Java Spec Doc Java 规范文档

But why does compiler raises a duplicate class error for但是为什么编译器会引发重复的类错误

class Test<String>{
    ...
}

class Test<Integer>{
    ...
}

Here binary name is same but argument types are different.What am I missing here?这里的二进制名称相同但参数类型不同。我在这里缺少什么?

It is not the argument, but the Type of a generic class.它不是参数,而是泛型类的Type This type information is removed at runtime, so from a JVM point of view it is identical.此类型信息在运行时被删除,因此从 JVM 的角度来看它是相同的。

You can read more about Generics and Type Erasure in the Oracle docs .您可以在Oracle 文档中阅读有关GenericsType Erasure更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM