如题,在Java里使用泛型是否会省掉类型强制转换,以提高效率?

解决方案 »

  1.   

    C#可以,但是据说Java的不能。
      

  2.   

    根据java的说明,范型的机制只是一种编译时机制,也就是说在运行时是不认范型和类型参数的,所有类都默认为object然后强制类型转换,所以范型主要还是用来进行编译时检查的。
      

  3.   

    修改一下,“应该是不同类型参数的范型类都共有一个运行时类”比如ArrayList<String>和ArrayList<Integer>在运行时是看作同一个类的,然后需要时都是强制类型转换下面抄了一段原文:
    7.1 A Generic Class is Shared by all its Invocations
    What does the following code fragment print?
    List <String> l1 = new ArrayList<String>();
    List<Integer> l2 = new ArrayList<Integer>();
    System.out.println(l1.getClass() == l2.getClass());
    You might be tempted to say false, but you’d be wrong. It prints true, because all
    instances of a generic class have the same run-time class, regardless of their actual type
    parameters.
    Indeed, what makes a class generic is the fact that it has the same behavior for all
    of its possible type parameters; the same class can be viewed as having many different
    types.
    As consequence, the static variables and methods of a class are also shared among
    all the instances. That is why it is illegal to refer to the type parameters of a type
    declaration in a static method or initializer, or in the declaration or initializer of a static
    variable.
      

  4.   

    原来如此,还有一个"run-time"类。