为什么不在运行时指定Java通用类型参数? [英] Why aren't Java generic type parameters reified at runtime?

查看:103
本文介绍了为什么不在运行时指定Java通用类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,C#和java在某些方面与泛型有所不同,其中之一是通用类型参数在运行时在C#/ .NET中可用,但在Java中不可用。为什么Java语言设计人员这样做?

解决方案

允许新代码与旧代码进行交互。 b
$ b

类型擦除


$ b


类型擦除使Java应用程序
能够使用泛型来维护二进制
与在
泛型之前创建的Java库和
应用程序的兼容性。

[...]



类型擦除存在,因此新代码
可能会继续与传统
代码接口。


有关相关问题,请查看 C#vs Java泛型


My understanding is that C# and java differ with respect to generics in some ways, one of which is that generic type parameters are available at runtime in C#/.NET but not in Java. Why did the Java language designers do it this way?

解决方案

To allow binary compatibility with pre-generics bytecode, therefore allowing new code to interface with old code.

From the Type Erasure page of The Java Tutorials:

Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

[...]

Type erasure exists so that new code may continue to interface with legacy code.

For a related question, take a look at C# vs Java generics.

这篇关于为什么不在运行时指定Java通用类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆