How does Java Object casting work behind the scene?(Java 对象转换如何在幕后工作?)
问题描述
可能重复:
Java cast 运算符是如何工作的?
Java 转换实现
我一直想知道对象转换在 Java 中是如何工作的.我理解原始类型更像是二进制表示级别,但是对象呢?是不是有点像 Polymorphism
或 dynamic binding
,因为一切都将在运行时确定?例如:
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()
场景中,您将收到一个 ClassCastException 因为一个 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
的 public void a()
将被调用,即使它是通过 Parent
的镜头看到的代码>类.但是,如果您要在 Child
中使用 Parent
没有的第二种方法(例如 public void b()
),如果不将对象转换回 Child
,您将 不 能够调用它.
... 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
),每个引用指向您使用 new 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
- 是Javapass-by-reference"吗?还是按值传递"?
这篇关于Java 对象转换如何在幕后工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 对象转换如何在幕后工作?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01