简体   繁体   English

为什么Java泛型必须擦除类型信息?

[英]why java generics have to erase the type information?

Java's generics would erase the type information after the source code was compiled. 编译源代码后,Java的泛型将擦除类型信息。 And i guess the "erase" is necessary because java only keep one copy of class no matter what the generic type is. 而且我猜想“擦除”是必要的,因为无论泛型是什么,java都只保留一个类的副本。 So List<String> or List<Number> are simply just one List. 所以List<String>List<Number>仅仅是一个列表。 Then I wonder if it possible that at the premise of keeping only one copy of certain class, the instance of the class stores the generic type information at the time it is created. 然后我想知道是否有可能在仅保留某个类的一个副本的前提下,该类的实例在创建类时存储泛型类型信息。

For instance: when we write: 例如:当我们写:

List<String> list = new List<String>. 

the compiler create an object of List along with a String's Class Object(meaning the Object String.class) accociated with the List, so that the generic object list can check the type information at runtime using the Class Object. 编译器会创建一个List对象,以及一个与List关联的String的Class Object(即Object String.class),以便通用对象列表可以在运行时使用Class Object检查类型信息。 Is it posssible or practicable? 这是可行的还是可行的?

I'm not entirely sure what you're asking specifically, but the big reason Java has to use erasure for generics is backwards compatibility. 我不确定您要问的是什么,但是Java必须对泛型使用Erase的主要原因是向后兼容。 If the behaviour of 如果行为

List list = new ArrayList(); //You can't do new list(), it's an interface

...was altered between versions, when you upgraded from say Java 1.4 to Java 5, you'd have all sorts of weird things going on potentially causing bugs where the code didn't behave in the same way as previously. ...在各个版本之间进行了更改,当您从Java 1.4升级到Java 5时,会发生各种奇怪的事情,这可能会导致错误,导致代码的行为与以前不同。 That's definitely a bad thing if that happens! 如果发生这种情况,那绝对是一件坏事!

If they didn't have to preserve backwards compatibility then yes, they could've done what they liked - we could've had nice reified generics and done a whole bunch of other stuff we couldn't do now. 如果他们不必保留向后兼容性,那么可以,他们可以按照自己喜欢的方式做-我们可以拥有很好的通用化泛型,还可以做很多其他我们现在不能做的事情。 There was a proposal (by Gafter I think) that would've allowed reified generics in Java in a backwards compatible way, but it would've involved creating new versions of all the classes that should have been generic. 有一个提议(我认为是Gafter提出的),该提议将允许Java中的通用泛型以向后兼容的方式出现,但这将涉及为所有本应泛型的类创建新版本。 That would've caused a load of mess with the API, so (for better or worse) they chose not to go down that route. 那会造成API混乱,所以(不管是好是坏)他们选择不沿着那条路走。

We are using List<String> l = new ArrayList<String>(); 我们正在使用List<String> l = new ArrayList<String>(); from java 5. Before it was not like this. 来自Java5。以前不是这样。

It was like List l = new ArrayList(); 就像List l = new ArrayList(); and user can add anything into it like Integer,String or any Object of user-type too.So Java people did not want to change the old code.So they just keep it upto compiler which can check this at compile time. 并且用户也可以向其中添加任何内容,例如Integer,String或任何用户类型的Object。因此Java人们不想更改旧代码。因此,他们将其保留给编译器,以便在编译时进行检查。

保留与Java5之前的代码的二进制向后兼容性。

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

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