gpt4 book ai didi

java - 为什么这个类型参数会保留在字节码中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:20 25 4
gpt4 key购买 nike

type erasure page说是

Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.

但是,对于下面的类:

public class Foo<E extends CharSequence> {
public E something;
}

javap -c Foo 打印:

public class Foo<E extends java.lang.CharSequence> {
public E something;
}

为什么类型参数没有替换为绑定(bind)(CharSequence),而是保留为E?

最佳答案

您打印的不是字节码。它是方法签名。将它放在那里是为了让编译器在编译其他调用它的类时可以强制执行类型安全。

关于java - 为什么这个类型参数会保留在字节码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com