- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,所以我正在学习我的第一个 java 认证,我不太清楚创建对象时到底发生了什么,我读到的一些解释指出构造函数返回对object 这对我来说是一个困惑的根源,因为根据我的理解,这是由 new 关键字完成的。我的问题是:
最佳答案
new Constructor(arguments)
的整个表达式被视为一个实例创建表达式,并且作为单独的 new
关键字和构造函数调用没有意义。
JLS,15.9.4描述了实例的实际创建,它由三个步骤组成:
new
时不适用)First, if the class instance creation expression is a qualified class instance creation expression, the qualifying primary expression is evaluated. If the qualifying expression evaluates to null, a NullPointerException is raised, and the class instance creation expression completes abruptly. If the qualifying expression completes abruptly, the class instance creation expression completes abruptly for the same reason.
Next, space is allocated for the new class instance. If there is insufficient space to allocate the object, evaluation of the class instance creation expression completes abruptly by throwing an OutOfMemoryError.
The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value (§4.12.5).
Next, the actual arguments to the constructor are evaluated, left-to-right. If any of the argument evaluations completes abruptly, any argument expressions to its right are not evaluated, and the class instance creation expression completes abruptly for the same reason.
Next, the selected constructor of the specified class type is invoked. This results in invoking at least one constructor for each superclass of the class type. This process can be directed by explicit constructor invocation statements (§8.8) and is specified in detail in §12.5.
The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.
是的,作为字节码,这一切都变成了一个invokespecial调用,新实例位于传递的参数堆栈的底部。请参阅此答案的底部。然而,从语义上讲,构造函数不会将新实例“传递”给它,但该实例在 Java 源代码中以 this
形式提供。
我编译和反汇编:
class Test{
public static void main(String args[]){
Integer s = new Integer(2);
}
}
这是结果:
public static void main(java.lang.String[]);
Code:
0: new #2 // class java/lang/Integer
3: dup
4: iconst_2
5: invokespecial #3 // Method java/lang/Integer."<init>":(I)V
8: astore_1
9: return
如您所见,首先使用 new
分配对象,它会默认填充所有字段。它是重复的,因此堆栈如下所示:
our new Integer
our new Integer
然后推送我传递给构造函数的常量值:
2
our new Integer
our new Integer
invokespecial
发生,它传递顶部两个 堆栈元素 - 新实例和构造函数的参数。
关于java - 'new' 关键字和构造函数到底是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25818996/
这对你们来说可能很简单,但由于我是java新手,所以我想知道实际上什么是 接下来的部分会发生什么? if (args.length > 0) { file = args[0]; } publi
在我的 View Controller 中,我将 UITapGestureRecognizer 添加到 self.view。我在 self.view 之上添加了一个小 View 。当我点击小 View
我今天尝试从 Obj-C 开始并转到 Swift,我正在阅读文档。我试图在 Swift 中创建一个简单的 IBOutlet,但它不断给我这些错误。 View Controller 没有初始化器 req
我正在尝试使用 VIM 完成(字典和当前缓冲区),但我遇到了问题?和 !在方法名称的末尾。我能以某种方式向 vim 解释方法名称(基本上是单词)最后只能有它,而且只有一个,即 method_name
我是一名优秀的程序员,十分优秀!