我正在阅读Java泛型中的Thought。在一个示例中(在“边界上的动作”段落中)

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是一个特殊的对象吗?

最佳答案

我将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.printObject。因此,不需要转换返回的值。 (checkcast java.lang.Object将始终通过!)

10-04 12:02