Java实例化 [英] Java Instantiation

查看:80
本文介绍了Java实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 当一个对象在Java中实例化时,真正进入内存的是什么?

  2. 是否包含父构造函数的副本?

  3. 为什么隐藏数据成员在转换时的行为与重写方法不同?

我理解通常给出的抽象解释是为了让你正确使用这些东西,但是JVM是如何做到的。

I understand the abstract explanations that are typically given to get you use this stuff correctly, but how does the JVM really do it.

推荐答案

实例化对象时,实际上只有非静态数据已创建,并且对该类型的引用创建它的对象。

When an object is instantiated, only the non-static data is actually "Created", along with a reference to the type of object that created it.

没有复制任何方法。

参考创建它的类实际上是一个指针调度表。每个可用于该类的方法都有一个指针。指针始终指向方法的正确(通常是对象树中最低/最具体的)实现。

The "Reference" to the class that created it is actually a pointer dispatch table. One pointer exists for each method that is available to the class. The pointers always point to the "correct" (usually the lowest/most specific in the object tree) implementation of the method.

如果你有一个顶级的那样调用另一个方法,但另一个方法已被覆盖,将调用重写的方法,因为这是表中指针指向的位置。由于这种机制,调用重写方法不应该花费更多时间而不是顶级方法。

That way if you have a top-level call to another method, but the other method has been overridden, the overridden method will be called because that's where the pointer in the table points. Because of this mechanism, it shouldn't take more time to call an overridden method than a top-level one.

指针表+成员变量是实例一个类。

The pointer table + member variables are the "Instance" of a class.

变量问题与完全不同的机制名称空间有关。变量根本不是Subclassed(它们不会进入调度表),但公共或受保护的变量可以被局部变量隐藏。这完全由编译器在编译时完成,与运行时对象实例无关。编译器确定你真正想要的对象,并将对它的引用填入你的代码。

The variable issue has to do with a completely different mechanism, "Name spaces". Variables aren't "Subclassed" at all (They don't go into the dispatch table), but public or protected variables can be hidden by local variables. This is all done by the compiler at compile time and has nothing to do with your runtime object instances. The compiler determines which object you really want and stuffs a reference to that into your code.

范围规则通常支持最近变量。使用相同名称的任何东西都将被忽略(阴影)以支持更接近的定义。

The scoping rules are generally to favor the "Nearest" variable. Anything further away with the same name will just be ignored (shadowed) in favor of the nearer definition.

如果您感兴趣,可以更详细地了解内存分配:所有OBJECTS都分配在Heap上(实际上比真正的堆更高效和漂亮,但是相同的概念。)变量总是指针 - Java永远不会复制对象,你总是复制指向该对象的指针。方法参数和局部变量的变量指针分配在堆栈上完成,但即使在堆栈上创建变量(指针),它们指向的对象仍然永远不会在堆栈上分配。

To get a little more specific about memory allocation if you are interested: all "OBJECTS" are allocated on the "Heap" (Actually something amazingly more efficient and beautiful than a true heap, but same concept.) Variables are always pointers--Java will never copy an object, you always copy a pointer to that object. Variable pointer allocations for method parameters and local variables are done on the stack, but even though the variable (pointer) is created on the stack, the objects they point to are still never allocated on the stack.

我很想写一个例子,但这已经太久了。如果你想让我输出一些带有扩展关系的类,以及他们的方法和数据如何影响生成的代码,我可以......只是问。

I'm tempted to write an example, but this is already too long. If you want me to type out a couple classes with an extends relationship and how their methods and data effect the code generated I can... just ask.

这篇关于Java实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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