Java中的向下类型转换

在Java中允许向上强制转换,但是向下强制转换会产生编译错误。

可以通过添加强制转换来删除编译错误,但无论如何都会在运行时中断。

在这种情况下,为什么Java允许向下强制转换,如果它不能在运行时执行?< br > 这个概念有什么实际用途吗?< / p >
public class demo {
public static void main(String a[]) {
B b = (B) new A(); // compiles with the cast,
// but runtime exception - java.lang.ClassCastException
}
}


class A {
public void draw() {
System.out.println("1");
}


public void draw1() {
System.out.println("2");
}
}


class B extends A {
public void draw() {
System.out.println("3");
}
public void draw2() {
System.out.println("4");
}
}
242724 次浏览

当向下类型转换有可能在运行时成功时,允许向下类型转换:

Object o = getSomeObject(),
String s = (String) o; // this is allowed because o could reference a String

在某些情况下,这不会成功:

Object o = new Object();
String s = (String) o; // this will fail at runtime, because o doesn't reference a String

当强制转换(例如最后一个)在运行时失败时,将抛出ClassCastException

在其他情况下,它将工作:

Object o = "a String";
String s = (String) o; // this will work, since o references a String

注意,一些类型转换在编译时将被禁止,因为它们根本不会成功:

Integer i = getSomeInteger();
String s = (String) i; // the compiler will not allow this, since i can never reference a String.

我相信这适用于所有静态类型的语言:

String s = "some string";
Object o = s; // ok
String x = o; // gives compile-time error, o is not neccessarily a string
String x = (String)o; // ok compile-time, but might give a runtime exception if o is not infact a String

类型转换有效地说:假设这是对强制转换类的引用,并将其作为引用使用。现在,假设o是真的一个整数,假设这是一个字符串没有意义,将会给出意想不到的结果,因此需要一个运行时检查和一个异常来通知运行时环境有错误。

在实际使用中,您可以编写处理更通用类的代码,但是如果您知道它是什么子类并且需要这样对待它,则可以将其转换为子类。一个典型的例子是重写Object.equals()。假设我们有一个Car类:

@Override
boolean equals(Object o) {
if(!(o instanceof Car)) return false;
Car other = (Car)o;
// compare this to other and return
}

用你的例子,你可以这样做:

public void doit(A a) {
if(a instanceof B) {
// needs to cast to B to access draw2 which isn't present in A
// note that this is probably not a good OO-design, but that would
// be out-of-scope for this discussion :)
((B)a).draw2();
}
a.draw();
}

在这种情况下,为什么Java允许向下强制转换,如果它不能在运行时执行?

我相信这是因为编译器无法在编译时知道强制转换是否会成功。对于您的示例,很容易看出强制转换将会失败,但在其他情况下就不那么清楚了。

例如,假设类型B、C和D都扩展类型A,然后方法public A getSomeA()根据随机生成的数字返回B、C或D的实例。编译器无法知道此方法将返回哪个确切的运行时类型,因此如果稍后将结果强制转换为B,则无法知道强制转换是否会成功(或失败)。因此,编译器必须假定强制转换会成功。

我们都可以看到,您提供的代码在运行时无法工作。这是因为我们知道表达式new A()可以从来没有是类型为B的对象。

但编译器并不这么认为。当编译器检查是否允许强制转换时,它只看到以下内容:

variable_of_type_B = (B)expression_of_type_A;

正如其他人所证明的那样,这种演员是完全合法的。右边的表达式很可能求值为B类型的对象。编译器认为AB具有子类型关系,因此在代码的"expression"视图下,强制转换可能有效。

编译器不考虑特殊情况,当它知道完全 expression_of_type_A将拥有什么对象类型。它只将静态类型视为A,并认为动态类型可以是AA的任何后代,包括B

@原始海报-见内联评论。

public class demo
{
public static void main(String a[])
{
B b = (B) new A(); // compiles with the cast, but runtime exception - java.lang.ClassCastException
//- A subclass variable cannot hold a reference to a superclass  variable. so, the above statement will not work.


//For downcast, what you need is a superclass ref containing a subclass object.
A superClassRef = new B();//just for the sake of illustration
B subClassRef = (B)superClassRef; // Valid downcast.
}
}


class A
{
public void draw()
{
System.out.println("1");
}


public void draw1()
{
System.out.println("2");
}
}


class B extends A
{
public void draw()
{
System.out.println("3");
}


public void draw2()
{
System.out.println("4");
}
}

向下转换适用于处理向上转换对象的情况。 向上转型:< / p >

int intValue = 10;
Object objValue = (Object) intvalue;

所以现在这个objValue变量总是可以向下转换为int,因为转换的对象是Integer

int oldIntValue = (Integer) objValue;
// can be done

但是因为objValue是一个对象,所以它不能被转换为String,因为int不能被转换为String

向下类型转换在下面的代码片段中非常有用,我一直在使用它。从而证明向下强制转换是有用的。

private static String printAll(LinkedList c)
{
Object arr[]=c.toArray();
String list_string="";
for(int i=0;i<c.size();i++)
{
String mn=(String)arr[i];
list_string+=(mn);
}
return list_string;
}

我在链表中存储字符串。 当我检索链表的元素时,返回对象。要以字符串(或任何其他类对象)的形式访问元素,向下强制转换有助于我。< / p > Java允许我们编译向下转换的代码,相信我们正在做错误的事情。 不过,如果人类犯了错误,它将在运行时被捕获

考虑下面的例子

public class ClastingDemo {


/**
* @param args
*/
public static void main(String[] args) {
AOne obj = new Bone();
((Bone) obj).method2();
}
}


class AOne {
public void method1() {
System.out.println("this is superclass");
}
}




class Bone extends AOne {


public void method2() {
System.out.println("this is subclass");
}
}
这里我们创建了子类Bone的对象,并将其赋值给超类AOne引用,现在超类引用不知道 关于在编译时的子类,即Bone中的方法method2。因此,我们需要将父类的引用向下转换为子类引用,以便结果引用可以知道子类中存在方法,即Bone

对象的向下转换是不可能的。 只有< / p >

DownCasting1 _downCasting1 = (DownCasting1)((DownCasting2)downCasting1);

是最低

class DownCasting0 {
public int qwe() {
System.out.println("DownCasting0");
return -0;
}
}


class DownCasting1 extends DownCasting0 {
public int qwe1() {
System.out.println("DownCasting1");
return -1;
}
}


class DownCasting2 extends DownCasting1 {
public int qwe2() {
System.out.println("DownCasting2");
return -2;
}
}


public class DownCasting {


public static void main(String[] args) {


try {
DownCasting0 downCasting0 = new DownCasting0();
DownCasting1 downCasting1 = new DownCasting1();
DownCasting2 downCasting2 = new DownCasting2();


DownCasting0 a1 = (DownCasting0) downCasting2;
a1.qwe(); //good


System.out.println(downCasting0 instanceof  DownCasting2);  //false
System.out.println(downCasting1 instanceof  DownCasting2);  //false
System.out.println(downCasting0 instanceof  DownCasting1);  //false


DownCasting2 _downCasting1= (DownCasting2)downCasting1;     //good
DownCasting1 __downCasting1 = (DownCasting1)_downCasting1;  //good
DownCasting2 a3 = (DownCasting2) downCasting0; // java.lang.ClassCastException


if(downCasting0 instanceof  DownCasting2){ //false
DownCasting2 a2 = (DownCasting2) downCasting0;
a2.qwe(); //error
}


byte b1 = 127;
short b2 =32_767;
int b3 = 2_147_483_647;
//          long _b4 = 9_223_372_036_854_775_807; //int large number max 2_147_483_647
long b4 = 9_223_372_036_854_775_807L;
//          float _b5 = 3.4e+038; //double default
float b5 = 3.4e+038F; //Sufficient for storing 6 to 7 decimal digits
double b6 = 1.7e+038;
double b7 = 1.7e+038D; //Sufficient for storing 15 decimal digits


long c1 = b3;
int c2 = (int)b4;


//int       4 bytes     Stores whole numbers from -2_147_483_648 to 2_147_483_647
//float     4 bytes     Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
float c3 = b3; //logic error
double c4 = b4; //logic error




} catch (Throwable e) {
e.printStackTrace();
}
}


}

要在Java中进行向下类型转换,并避免运行时异常,请参考以下代码:

if (animal instanceof Dog) {
Dog dogObject = (Dog) animal;
}

这里,Animal是父类,Dog是子类 >是一个关键字,用于检查引用变量是否包含给定类型的对象引用。

我来告诉你为什么会这样。首先,你必须理解JVM如何支持当我们使用向下强制转换将父类分配给子类时,因为reference。例如,考虑下面的代码。

A is the super type any class that extends from it and can store the reference B class.
A a =new B();
When you assign a reference variable into the child class jvm will understand that since A can store the reference of B class that is why you can do it.
B b=(B)b;
  1. 原因被称为编译时错误,以及为什么你不能直接将父类分配到子类,因为没有任何扩展关系。注意,强制转换只发生在名为extends的键上,这就是为什么你会收到编译时错误。
  2. 另一个被运行时称为ClassCastException的原因是jvm直接接受规则,这是可以的,我接受这是真的,但jvm之后会理解,到运行时,它不会存储任何子类的引用,当代码由编写语法代码的程序员编写时。