小编Ram*_*lfc的帖子

Java中的泛型类型擦除

这是代码:

public class Main {
    public static void main(String[] args) {
        Gen<Integer> g = new Gen<Integer>(5);

        System.out.println(g.getClass());
        System.out.println(g.ob.getClass());
    }
}

class Gen<T> {
    T ob;

    public Gen(T x) {
        ob = x;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出

class Gen               // This I understand
class java.lang.Integer // But if type erasure is happening, shouldn't this be java.lang.Object?
Run Code Online (Sandbox Code Playgroud)

我知道Type参数T在运行时被擦除了,但是为什么ob在运行时存活的类型参数呢?

java generics type-erasure

7
推荐指数
1
解决办法
340
查看次数

在java中传递注释参数而不指定名称

有没有办法像这样传递注释参数:

@MyAnnotation(4)

代替

@MyAnnotation( value = 4)

java annotations

4
推荐指数
1
解决办法
353
查看次数

标签 统计

java ×2

annotations ×1

generics ×1

type-erasure ×1