Generic types in Java -
is right code
list<integer> test2 = new arraylist<integer>(); test2.add(343); int x2 = test2.get(0);
in compile time converted this
list test = new arraylist(); test.add(343); int x = (integer)test.get(0);
something similar autoboxing...
let's try. take class 2 methods absolutely same thing , without generics , autoboxing:
public class generictest{ // use generics , auto-boxing // java 1.5 or higher required public void generic(){ final list<integer> test2 = new arraylist<integer>(); test2.add(343); final int x2 = test2.get(0); } // use neither generics nor auto-boxing, // should java 1.4-compatible public void nongeneric(){ final list test2 = new arraylist(); test2.add(integer.valueof(343)); final int x2 = ((integer) test2.get(0)).intvalue(); } }
here's byte code:
// compiled generictest.java (version 1.6 : 50.0, super bit) public class generictest { // method descriptor #6 ()v // stack: 1, locals: 1 public generictest(); 0 aload_0 [this] 1 invokespecial java.lang.object() [8] 4 return line numbers: [pc: 0, line: 4] local variable table: [pc: 0, pc: 5] local: index: 0 type: generictest // method descriptor #6 ()v // stack: 2, locals: 3 public void generic(); 0 new java.util.arraylist [16] 3 dup 4 invokespecial java.util.arraylist() [18] 7 astore_1 [test2] 8 aload_1 [test2] 9 sipush 343 12 invokestatic java.lang.integer.valueof(int) : java.lang.integer [19] 15 invokeinterface java.util.list.add(java.lang.object) : boolean [25] [nargs: 2] 20 pop 21 aload_1 [test2] 22 iconst_0 23 invokeinterface java.util.list.get(int) : java.lang.object [31] [nargs: 2] 28 checkcast java.lang.integer [20] 31 invokevirtual java.lang.integer.intvalue() : int [35] 34 istore_2 [x2] 35 return line numbers: [pc: 0, line: 7] [pc: 8, line: 8] [pc: 21, line: 9] [pc: 35, line: 10] local variable table: [pc: 0, pc: 36] local: index: 0 type: generictest [pc: 8, pc: 36] local: test2 index: 1 type: java.util.list [pc: 35, pc: 36] local: x2 index: 2 type: int local variable type table: [pc: 8, pc: 36] local: test2 index: 1 type: java.util.list<java.lang.integer> // method descriptor #6 ()v // stack: 2, locals: 3 public void nongeneric(); 0 new java.util.arraylist [16] 3 dup 4 invokespecial java.util.arraylist() [18] 7 astore_1 [test2] 8 aload_1 [test2] 9 sipush 343 12 invokestatic java.lang.integer.valueof(int) : java.lang.integer [19] 15 invokeinterface java.util.list.add(java.lang.object) : boolean [25] [nargs: 2] 20 pop 21 aload_1 [test2] 22 iconst_0 23 invokeinterface java.util.list.get(int) : java.lang.object [31] [nargs: 2] 28 checkcast java.lang.integer [20] 31 invokevirtual java.lang.integer.intvalue() : int [35] 34 istore_2 [x2] 35 return line numbers: [pc: 0, line: 13] [pc: 8, line: 14] [pc: 21, line: 15] [pc: 35, line: 16] local variable table: [pc: 0, pc: 36] local: index: 0 type: generictest [pc: 8, pc: 36] local: test2 index: 1 type: java.util.list [pc: 35, pc: 36] local: x2 index: 2 type: int }
i don't see obvious difference between generic , non-generic version, in fact here's diff
result 2 methods:
< public void generic(); --- > public void nongeneric(); 19,22c19,22 < [pc: 0, line: 7] < [pc: 8, line: 8] < [pc: 21, line: 9] < [pc: 35, line: 10] --- > [pc: 0, line: 13] > [pc: 8, line: 14] > [pc: 21, line: 15] > [pc: 35, line: 16] 27,28d26 < local variable type table: < [pc: 8, pc: 36] local: test2 index: 1 type: java.util.list<java.lang.integer>
as can see, difference in line numbers , local variable table.
Comments
Post a Comment