gpt4 book ai didi

java - 'new' 关键字和构造函数到底是如何工作的

转载 作者:行者123 更新时间:2023-12-01 12:32:41 24 4
gpt4 key购买 nike

好的,所以我正在学习我的第一个 java 认证,我不太清楚创建对象时到底发生了什么,我读到的一些解释指出构造函数返回对object 这对我来说是一个困惑的根源,因为根据我的理解,这是由 new 关键字完成的。我的问题是:

  1. 对对象的引用实际上来自哪里,“new”关键字还是构造函数?
  2. 当使用“new”关键字创建对象时,是否会隐式将此对象传递给构造函数?
  3. 当使用“new”关键字创建一个对象时,它是否只是一个java对象,直到它被传递给构造函数,例如。人我 = new Person();该对象是在创建后立即与类 person 关联,还是仅在将其传递给构造函数之后才关联

最佳答案

  1. new Constructor(arguments) 的整个表达式被视为一个实例创建表达式,并且作为单独的 new 关键字和构造函数调用没有意义。

    JLS,15.9.4描述了实例的实际创建,它由三个步骤组成:

    1. 限定表达式(使用 new 时不适用)
    2. 在内存中为实例分配空间,并将字段设置为默认值。
    3. 计算构造函数的参数并调用构造函数。

    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.

  2. 是的,作为字节码,这一切都变成了一个invokespecial调用,新实例位于传递的参数堆栈的底部。请参阅此答案的底部。然而,从语义上讲,构造函数不会将新实例“传递”给它,但该实例在 Java 源代码中以 this 形式提供。

  3. 这个问题没有语义意义,因为在构造函数返回之前,对象对于外界来说并不“存在”。如果构造突然终止,则该对象实际上并未创建并且不可用。 但是,至少在 OpenJDK JVM 的内存中,对象的类型以及它扩展/实现的任何类型都会写入内存 header 。但是,并不能保证所有实现都如此。

反汇编:

我编译和反汇编:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com