ClassCastException vs.“无法投射”编译错误 [英] ClassCastException vs. "cannot cast" compilation error

查看:188
本文介绍了ClassCastException vs.“无法投射”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习我的OCA Java SE 7程序员我考试,所以新手问题。我有一个例子问题我不明白。以下代码编译,但在运行时提供ClassCastException:

  interface Roamable {
}

class Phone {
}

public class Tablet extends电话实现Roamable {
public static void main(String ... args){
Roamable var = Roamable)new Phone();
}
}

当我改变 Roamable var =(Roamable)new Phone(); into Roamable var =(Roamable)new String(); / p>

两个问题:


  1. 为什么上面的代码完全编译?电话似乎与Roamable无关?

  2. 为什么代码用 new Phone()编译, new String()


解决方案


为什么上面的代码完全编译?电话似乎与
无关?对我来说是roamable?


是因为 Roamable 是一个接口,它可能导致运行时异常而不是编译时异常,因为即使 Phone 没有实现 Roamable Phone 的子类,因此编译器无法知道它,但在运行时。



它已经在java语言规范中定义。 在此查看我的回答


为什么代码用新的Phone()编译,但不编译
with new String()?


因为 class String 包中的 public final class jls 8.1.1.2 final class 部分:声明为 final 的类不能扩展,因此它赢了没有任何子类。因此,编译器已经知道 String 不能扩展:因此没有子类的存在可以实现 interface Roamable

让我们假设 B 是实现接口的子类 A T



现在声明:

  T t =一个(); 

本质上相同:

  A aObj = new A(); 
T t =(T)aObj; //运行时异常发生

在结束之前,让我们做同样的事情对象 B

  A aObj = new B 
T t =(T)aObj; //没有异常发生。

所以,这里的超类和子类的真正原因是引用。在第二个代码示例中的 aObj 类也是类 A 的实例,但它也是类 B 已实现 T


Studying for my OCA Java SE 7 Programmer I exam, so newbie question. I have an example question I do not understand. The following code compiles, but gives a ClassCastException at runtime:

interface Roamable {
}

class Phone {
}

public class Tablet extends Phone implements Roamable {
    public static void main(String... args) {
        Roamable var = (Roamable) new Phone();
    }
}

When I change Roamable var = (Roamable) new Phone(); into Roamable var = (Roamable) new String(); I get a compilation error right away.

Two questions:

  1. Why does the code above compile at all? Phone seems unrelated to Roamable to me?
  2. Why does the code compile with new Phone(), but doesn't it compile with new String()?

解决方案

Why does the code above compile at all? Phone seems unrelated to Roamable to me?

yes because Roamable is an interface it might cause a Run-time exception but not compile time exception, because even if Phone doesn't implement Roamable, a subclass of Phone might, hence the compiler has no way to know it but at the Run time.

It is already defined in java language specification. Check out my answer here.

Why does the code compile with new Phone(), but doesn't it compile with new String()?

Because class String is declared as public final class in java.lang package. As specified in jls 8.1.1.2 final class section: a class declared as final can't be extended and hence it won't have any subclass. So, the compiler already knows that String can't be extended: hence no subclass's existence is possible to implement interface Roamable

Edit: (With response to your below comment)

Let us assume that B is a subclass of A which implements an interface T.

Now an statement :

   T t = (T)new A();

is essentially same as:

   A aObj = new A() ;
   T t = (T)aObj ; // a run-time exception happen

before running into conclusion, let us do the same thing with an object of B:

   A aObj = new B();
   T t = (T)aObj; // no exception happen.

so, the real reason with super class and sub class here is the reference. The aObj class in this second code example is also an instance of class A but it is also an instance of class B which has implemented T.

这篇关于ClassCastException vs.“无法投射”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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