泛型、数组和 ClassCastException
我想这里一定发生了一些我不知道的微妙事情。 请考虑以下情况:
public class Foo<T> {
private T[] a = (T[]) new Object[5];
public Foo() {
// Add some elements to a
}
public T[] getA() {
return a;
}
}
假设您的 main 方法包含以下内容:
Foo<Double> f = new Foo<Double>();
Double[] d = f.getA();
您将收到 CastClassException
,其中包含消息 java.lang.Object
无法转换为 java.lang .双
。
谁能告诉我为什么? 我对 ClassCastException 的理解是,当您尝试将对象强制转换为无法强制转换的类型时,会抛出该异常。 也就是说,对于它不是实例的子类(引用文档)。 例如:
Object o = new Double(3.);
Double d = (Double) o; // Working cast
String s = (String) o; // ClassCastException
看来我能做到这一点。 如果 a
只是一个 T
而不是数组 T[]
,我们可以获取 a
并强制转换它,而无需一个问题。 为什么数组会破坏这个?
谢谢。
I think there must be something subtle going on here that I don't know about. Consider the following:
public class Foo<T> {
private T[] a = (T[]) new Object[5];
public Foo() {
// Add some elements to a
}
public T[] getA() {
return a;
}
}
Suppose that your main method contains the following:
Foo<Double> f = new Foo<Double>();
Double[] d = f.getA();
You will get a CastClassException
with the message java.lang.Object
cannot be cast to java.lang.Double
.
Can anyone tell me why? My understanding of ClassCastException
is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:
Object o = new Double(3.);
Double d = (Double) o; // Working cast
String s = (String) o; // ClassCastException
And it seems I can do this. If a
was just a T
instead of an array T[]
, we can get a
and cast it without a problem. Why do arrays break this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用此版本的泛型类 Foo 时,对于成员变量
a
,编译器实际上会采用以下行:并将
T
替换为Double 得到这个:
你不能从 Object 转换为 Double,因此会出现 ClassCastException。
更新和说明:实际上,在运行一些测试代码后,ClassCastException 比这更微妙。 例如,这个 main 方法将正常工作,没有任何异常:
就会出现问题:
当您尝试将
f.getA()
分配给Double[]
类型的引用时, 是因为成员变量a
的类型信息在运行时被删除。 泛型仅在编译时提供类型安全(我在最初的帖子中以某种方式忽略了这一点)。 所以问题并不是因为在运行时这段代码实际上是
方法
getA()
的结果时出现的问题,该方法在运行时实际上返回一个Object[]
,被分配给类型Double[]
的引用 - 此语句会抛出 ClassCastException,因为 Object 无法转换为 Double。更新 2:回答最后一个问题“为什么数组会破坏这个?” 答案是因为语言规范不支持通用数组创建。 请参阅此论坛帖子了解更多信息 - 为了向后兼容,什么都不知道关于运行时 T 的类型。
When you use this version of the generic class Foo, then for the member variable
a
, the compiler is essentially taking this line:and replacing
T
withDouble
to get this:You cannot cast from Object to Double, hence the ClassCastException.
Update and Clarification: Actually, after running some test code, the ClassCastException is more subtle than this. For example, this main method will work fine without any exception:
The problem occurs when you attempt to assign
f.getA()
to a reference of typeDouble[]
:This is because the type-information about the member variable
a
is erased at runtime. Generics only provide type-safety at compile-time (I was somehow ignoring this in my initial post). So the problem is notbecause at run-time this code is really
The problem occurs when the result of method
getA()
, which at runtime actually returns anObject[]
, is assigned to a reference of typeDouble[]
- this statement throws the ClassCastException because Object cannot be cast to Double.Update 2: to answer your final question "why do arrays break this?" The answer is because the language specification does not support generic array creation. See this forum post for more - in order to be backwards compatible, nothing is known about the type of T at runtime.
@mattb 的解释可能存在一些小错误。
错误不是
这是:
[L 表示数组。 也就是说,错误在于对象的数组无法转换为 Double 数组。 这和下面的情况是一样的:
这显然是不允许的。
对于创建类型安全数组的问题,另一种选择是在 init 中排除类对象并使用 Array.newInstance:
There may be some small errors in @mattb's explanation.
The error is not
It is:
The [L means an array. That is, the error is that an array of Objects cannot be cast to an array of Double. This is the same case as following:
This is obviously not allowed.
For your issue of creating typesafe arrays, another alternative is to except a class object in init and use Array.newInstance:
@matt b:谢谢你的回答! 很有帮助。
我为感兴趣的人找到了一种解决方法:为 getA 方法提供一个要填充的初始化数组。 这样类型信息就可用了。
然后对于你的主要方法:
@matt b: Thanks for the answer! Very helpful.
I have found a workaround for those interested: give the getA method an initialized array to populate. That way the type info is available.
Then for your main method: