Java 对象转换在幕后如何工作? [英] How does Java Object casting work behind the scene?

查看:24
本文介绍了Java 对象转换在幕后如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
Java 强制转换运算符的工作原理是什么?
Java 转换实现

我一直想知道对象转换在 Java 中是如何工作的.我理解原始类型更像是二进制表示级别,但是对象呢?它有点像 Polymorphismdynamic binding ,因为一切都将在运行时确定?例如:

class Parent{空 A(){}}类子扩展父{@覆盖空 A(){}}父 p = new Parent();子 c = (子) p;

这在幕后是如何运作的?它是否创建了 Child 的新实例?而且,如果您尝试投射会发生什么:

Child b = (Child) new Object();

最后一个,将原语转换为包装类时:

Double d = (Double) 3.3;

我知道你没有必要施展它,但如果你施展了呢?后端有什么重要的事情发生吗?

解决方案

当您使用显式转换时,系统中不会创建新对象(除了在最后一种情况下,您将原始类型转换为 对象包装器,因为 double 不是像 Double 这样的对象).请注意,由于 Java 的 自动装箱功能.

在您的 (Child) new Object() 场景中,您将收到一个 ClassCastException 因为 Object 不是 Child(尽管相反是正确的).>

第一个场景的答案是最复杂的.本质上,父类被视为可能的接口.当您将 Child 转换为 Parent 时,只有 Parent API 可用.但是,仍然会调用被覆盖的方法.所以,如果你这样做:

Parent p = (Parent) new Child();p.a();

... Childpublic void a() 将被调用,即使它是通过 Parent 的镜头看到的代码>类.但是,如果您要在 Child 中使用 Parent 没有的第二种方法(例如,例如 public void b()),如果不将对象转换回 Child,您将不能调用它.

幕后",如你所说,唯一创建的新事物是另一个指向同一对象的对象引用.对于同一个单一对象,您可以拥有任意数量的引用.考虑这个例子:

Parent p = new Parent();父 p1 = p;父 p2 = p;父 p3 = p2;

这里有四个引用(pp1p2p3),每个引用指向您使用 new Parent() 声明创建的同一对象.

不过,我可能会在哲学观点上争论,当您说 Parent p = something 时,这种新引用的创建实际上是明确的,而不是在幕后.

链接:

Possible Duplicate:
How does the Java cast operator work?
Java casting implementation

I am always wondering how object casting works in Java. I understand for primitive type it will be more like in binary representation level, but what about Object? Is it kind of like Polymorphism or dynamic binding in that everything will be determined at runtime? For example:

class Parent{
     void A(){}
}
class Child extends Parent{
     @Override
     void A(){}
}

Parent p = new Parent();
Child c = (Child) p;

How does this work behind the scene? Does it create a new instance of Child? And also, what happens if you try to cast:

Child b = (Child) new Object();

And last one, when casting a primitive to a wrapper class:

Double d = (Double) 3.3;

I know you don't necessary need to cast it, but what if you do? Is there anything significant that happens on the backend?

解决方案

No new objects are created in the system when you use explicit casting (except in your last case, where you cast a primitive type to an object wrapper, since double is not an object like Double is). Note that this explicit cast isn't necessary due to Java's autoboxing feature.

In your (Child) new Object() scenario, you will receive a ClassCastException because an Object is not a Child (although the opposite is true).

The answer to your first scenario is the most complicated. Essentially the parent class is treated like an interface might be. When you cast the Child to the Parent, only the Parent API is available. However, the overridden method will still be called. So, if you do:

Parent p = (Parent) new Child();
p.a();

... the Child's public void a() will be called, even though it is being seen through the lens of the Parent class. However if you were to have a second method in the Child that the Parent does not have (let's say public void b() for instance), you would not be able to call that without casting the object back to a Child.

"Behind the scenes", as you say, the only new thing that is created is another object reference which points to the same object. You can have as many references as you like to the same, singular object. Consider this example:

Parent p = new Parent();
Parent p1 = p;
Parent p2 = p;
Parent p3 = p2;

Here, there are four references (p, p1, p2, and p3) each of which points to the same object you created with the new Parent() declaration.

I would probably argue on the philosophical point, though, that this creation of new references is actually explicit rather than behind the scenes when you say Parent p = something.

Links:

这篇关于Java 对象转换在幕后如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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