Java中的泛型类型

时间:2011-02-23 16:32:38

标签: java generics

这段代码是否正确

List<Integer> test2 = new ArrayList<Integer>();
test2.add(343);
int x2 = test2.get(0);

在编译时将转换为此

List test = new ArrayList();
test.add(343);
int x = (Integer)test.get(0);

与自动装箱相似的东西......

4 个答案:

答案 0 :(得分:3)

是的,除此之外 “test2.add(343);”不会改为“test.add(43);”

答案 1 :(得分:2)

我们试试吧。在这个类中使用两种方法,这些方法在使用和不使用泛型和自动装箱时完全相同:

public class GenericTest{

    // use generics and 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,
    // this should be 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();
    }
}

这是字节代码:

// Compiled from 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: this 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: this 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: this 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
}

我没有看到泛型和非泛型版本之间有任何明显的区别,实际上这是两种方法的diff结果:

<  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>

如您所见,唯一的区别在于行号和局部变量表。

答案 2 :(得分:1)

[[强制性填料实现&gt; 30个字符]]

答案 3 :(得分:1)

是:

List test = new ArrayList();
test.add(Integer.valueOf(343));
int x = ((Integer) test.get(0)).intValue();
相关问题