泛型、数组和 ClassCastException [英] Generics, arrays, and the ClassCastException

查看:16
本文介绍了泛型、数组和 ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这里一定有一些我不知道的微妙之处.考虑以下几点:

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();

您将收到带有消息 java.lang.Object cannot be cast to java.lang.DoubleCastClassException.

You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.

谁能告诉我为什么?我对 ClassCastException 的理解是,当您尝试将对象转换为无法转换的类型时会抛出该异常.也就是说,对于它不是实例的子类(引用文档).例如:

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

看来我可以做到这一点.如果 a 只是一个 T 而不是数组 T[],我们可以得到 a 并在没有一个问题.为什么数组会破坏这个?

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?

谢谢.

推荐答案

Foo<Double> f = new Foo<Double>();

当你使用这个版本的泛型类 Foo 时,对于成员变量 a,编译器本质上是采取这一行:

When you use this version of the generic class Foo, then for the member variable a, the compiler is essentially taking this line:

private T[] a = (T[]) new Object[5];

并用 Double 替换 T 得到这个:

and replacing T with Double to get this:

private Double[] a = (Double[]) new Object[5];

您不能从 Object 强制转换为 Double,因此会出现 ClassCastException.

You cannot cast from Object to Double, hence the ClassCastException.

更新和澄清:实际上,在运行一些测试代码后,ClassCastException 比这更微妙.例如,这个 main 方法可以正常工作,没有任何异常:

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:

public static void main(String[] args) {
    Foo<Double> f = new Foo<Double>();
    System.out.println(f.getA());
}

当您尝试将 f.getA() 分配给 Double[] 类型的引用时会出现问题:

The problem occurs when you attempt to assign f.getA() to a reference of type Double[]:

public static void main(String[] args) {
    Foo<Double> f = new Foo<Double>();
    Double[] a2 = f.getA(); // throws ClassCastException
    System.out.println(a2);
}

这是因为成员变量 a 的类型信息在运行时被删除了.泛型只在编译时提供类型安全(我在最初的帖子中以某种方式忽略了这一点).所以问题不是

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 not

private T[] a = (T[]) new Object[5];

因为在运行时这段代码确实是

because at run-time this code is really

private Object[] a = new Object[5];

当方法 getA()(在运行时实际返回一个 Object[])的结果被分配给 Double 类型的引用时,就会出现问题[] - 此语句抛出 ClassCastException,因为 Object 不能转换为 Double.

The problem occurs when the result of method getA(), which at runtime actually returns an Object[], is assigned to a reference of type Double[] - this statement throws the ClassCastException because Object cannot be cast to Double.

更新 2:回答你的最后一个问题为什么数组会破坏这个?"答案是因为语言规范不支持泛型数组创建.查看此论坛帖子了解更多 - 为了向后兼容,什么都不知道关于运行时 T 的类型.

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.

这篇关于泛型、数组和 ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆