为什么在java泛型中仅在运行时检查String类型返回值

时间:2011-06-29 17:30:11

标签: java generics

我在阅读Thinking in java,Generics。在一个例子中(在“边界处的行动”段落中)

public class GenericHolder<T> {
    private T obj;
    public void set(T obj) { this.obj = obj; }
    public T get() { return obj; }
    public static void main(String[] args) {
        GenericHolder<String> holder =
        new GenericHolder<String>();
        holder.set("Item");
        String s = holder.get();
    }
} ///:~


public void set(java.lang.Object);
    0: aload_0
    1: aload_1
    2: putfield #2; //Field obj:Object;
    5: return
public java.lang.Object get();
    0: aload_0
    1: getfield #2; //Field obj:Object;
    4: areturn
public static void main(java.lang.String[]);
    470 Thinking in Java Bruce Eckel
    0: new #3; //class GenericHolder
    3: dup
    4: invokespecial #4; //Method "<init>":()V
    7: astore_1
    8: aload_1
    9: ldc #5; //String Item
    11: invokevirtual #6; //Method set:(Object;)V
    14: aload_1
    15: invokevirtual #7; //Method get:()Object;
    18: checkcast #8; //class java/lang/String
    21: astore_2
    22: return

根据反汇编的代码行18,编译器添加校验和代码。我想知道是否总是对通用的这种checkcast。我将String替换为Integer并再次尝试,但我最终没有找到checkcast代码。对象类型也是如此。

有人可以解释一下吗? Java中的String是一个特殊的对象吗?

1 个答案:

答案 0 :(得分:5)

  

我将String替换为Integer并再次尝试,但我最终没有找到checkcast代码。

对我来说,它也为checkcast生成Integer

  0: new    #3; //class GenericHolder
  3: dup
  4: invokespecial  #4; //Method GenericHolder."<init>":()V
  7: astore_1
  8: aload_1
  9: iconst_5
 10: invokestatic   #5; //Method java/lang/Integer.valueOf:(I)
 13: invokevirtual  #6; //Method GenericHolder.set:(Ljava/lang/Object;)V
 16: aload_1
 17: invokevirtual  #7; //Method GenericHolder.get:()Ljava/lang/Object;
 20: checkcast  #8; //class java/lang/Integer
 23: astore_2
 24: return

如您所见,get方法具有签名Ljava/lang/Object;,即checkcast是为了确保返回的Object确实是Integer }。


修改:在您发布的评论代码中:

0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: new #3; //class b/TestG
6: dup
7: new #4; //class java/lang/Integer
10: dup
11: iconst_2
12: invokespecial #5; //Method java/lang/Integer."<init>":(I)V
15: invokespecial #6; //Method b/TestG."<init>":(Ljava/lang/Object;)V
18: invokevirtual #7; //Method b/TestG.getT:()Ljava/lang/Object;
21: invokevirtual #8; //Method java/io/PrintStream.print:(Ljava/lang/Objec t;)V 
24: return

您将返回的值传递给接受PrintStream.print的{​​{1}}。因此,不需要转换返回的值。 (Object总会通过!)