我知道 java 不应该支持原始类型的泛型参数,而且确实是这样的:
Vector<byte> test;
将无法编译。
但是,我在程序中不小心操作了一下,发现实际上可以创建一个具有原始类型的通用对象(技术如下所示)
此外,java 错误地允许将此实例分配给类型为 Vector<Byte>
的变量。当打印语句显示时, byte.class 和 Byte.class 是两个独立的野兽。因此,尝试对对象进行调用会导致意外和奇怪的行为/错误。
这是一个java错误吗?或者这种疯狂有什么押韵或理由吗?似乎即使 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);
}
}
最佳答案
两者Byte.class
和 Byte.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
时会在运行时引发哪些异常? ?
关于Java 允许泛型中的原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670576/