Java允许泛型中的原始类型

时间:2013-09-07 06:43:55

标签: java generics

我知道java不应该支持基本类型的泛型参数,而且确实如下:

Vector<byte> test;

将无法编译。

然而,我偶然在一个程序中执行了一些小手,我发现实际上可以创建一个具有基本类型的通用对象(如下所示的技术)

此外,java错误地允许将此实例分配给类型为Vector<Byte>的变量,而当print语句显示时,byte.class和Byte.class是两个独立的动物。因此,尝试对对象进行调用会导致意外和奇怪的行为/错误。

这是一个java bug吗?还是有些押韵或理由让这种疯狂?似乎即使java允许创建基本类型泛型的意外行为,它们也不应该被赋予包装类型的泛型,该类型与原语具有不同的类。

import java.util.Vector;

public class Test
{
    //the trick here is that I am basing the return type of
    //the vector off of the type that was given as the generic
    //argument for the instance of the reflections type Class,
    //however the the class given by byte.class yields a non-class
    //type in the generic, and hence a Vector is created with a
    //primitive type
    public static <Type> Vector<Type> createTypedVector(Class<Type> type)
    {
        return new Vector<Type>(0,1);
    }

    public static void main(String ... args)
    {
        //these lines are to demonstrate that 'byte' and 'Byte'
        //are 2 different class types
        System.out.println(byte.class);
        System.out.println(Byte.class);

        //this is where I create an instance of type Vector<byte>
        //and assign it to a variable of type Vector<Byte>
        Vector<Byte> primitiveTypedGenericObject = createTypedVector(byte.class);

        //this line causes unexpected exceptions to be thrown
        //because primitiveTypedGenericObject is not actually type
        //Vector<Byte>, but rather Vector<byte>
        primitiveTypedGenericObject.set(0,(byte)0xFF);

    }

}

3 个答案:

答案 0 :(得分:19)

Byte.classByte.TYPE都是Class<Byte>个对象。后者仅用于区分原始类型和对象类型。

实际上,Byte.TYPE定义为:

public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

getPrimitiveClass是一个不透明的方法,它从VM中检索类型,因此我们无法进一步调查它。

所以,即使你认为你正在传递一个原始数据类型Class,因为它们不存在(为什么它们应该,因为它们指的是根据对象的Java类型系统可以打字的东西,它不是' t包括基本类型,直到它们被装入包装类),你创建了一个Vector<Byte>

但最终这并不重要,在达到运行时执行时,类型注释被删除,而泛型类型并不意味着什么。每当您添加byte时,它都会自动生成一个Byte对象,就是这样。

我目前无法测试您的代码,在向Vector添加项目时会在运行时抛出哪些异常?

答案 1 :(得分:15)

你偶然发现了自动装箱和拆箱。见http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

坚果壳版

List<Integer> list = new List<Integer>(); 
list.add(1); // works

byte.classint.class等解析为ByteInteger等。请参阅Java Language Specification, 15.8.2

  

15.8.2。类文字

     

...

     

p.class的类型,其中p是基本类型的名称(§4.2),是Class<B>,其中B是拳击转换后类型p的表达式的类型(§5.1.7)。

     

void.class§8.4.5)的类型为Class<Void>

答案 2 :(得分:8)

没有!这不是错误。它被称为 Autoboxing 。当您将字节传递给期望 Byte 的泛型方法时,编译器会自动将其自动提取到Byte,这是Object的一个实例。该操作的对立面称为自动取消装箱,这就是为什么类似下面所示的操作是合法的。

int a = new Integer(5);
Integer b = 5;
相关问题