问题描述
我总是想知道如何在Java 。我理解为原始类型它将更像是在二进制表示水平,但是对象?是否类似于多态性
或动态绑定
,因为一切都将在运行时确定?例如:
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;
这是如何在幕后工作的?是否创建 Child
的新实例?而且,如果你尝试投射会发生什么:
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?
推荐答案
当使用显式转换时,不会在系统中创建任何新对象在您最后一种情况下,将原始类型投射到,因为 double
不是
Double
的对象)。请注意,由于Java的,此显式强制转换不是必需的。
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.
在(Child)new Object()
场景中,您将收到,因为对象
不是 Child
(虽然相反是真的)。
In your (Child) new Object()
scenario, you will receive a ClassCastException because an Object
is not a Child
(although the opposite is true).
你的第一个场景的答案是最复杂的。基本上父类被视为接口可能是。将 Child
转换到 Parent
时,只有 Parent
API可用。但是,。所以,如果你这样做:
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();
... Child
code> public void a()将被调用,即使它通过 Parent
类的镜头被看到。但是如果你在 Child
中有第二个方法, Parent
没有(比方说 public void b()
),你将不能能够调用该对象,而不将该对象转回 code>。
... 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;
这里有四个参考( p
, p1
, p2
和 p3
)指向使用新Parent()
声明创建的同一对象。
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.
我可能会在哲学但是,当你说 Parent p = something
时,这种新引用的创建实际上是显式的而不是幕后。
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
.
链接:
- http://en.wikipedia.org/wiki/Primitive_wrapper_class
- http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html
- http://docs.oracle.com/javase/tutorial/java/IandI/override.html
- Is Java "pass-by-reference"?
这篇关于Java对象转换如何在幕后工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!