开发者

Why only String type return value are checked at run time in java generic

I am reading Thinking in java , Generics. In one example(at paragraph "The action at the boundaries")

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;
   开发者_如何学Go 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

According to the disassembled code line 18, the compiler adds the checkcast code. I want to know if it is always to such a checkcast for generic. I replaced the String to Integer and tried again, but I didn't find the checkcast code at last. So does the Object type.

Could someone explain that? Is String in Java is a special Object?


I replace the String to Integer and tried again, but I didn't find the checkcast code at last.

For me it produces a checkcast also for 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

As you can see, the get method has the signature Ljava/lang/Object; i.e., the checkcast is to make sure that the Object returned is indeed an Integer.


Edit: In the code you posted as a comment:

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

you pass on the returned value to PrintStream.print which accepts an Object. Thus, there is no need to cast the returned value. (checkcast java.lang.Object would always go through!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜