1. Overview
1.概述
In this quick tutorial, we’re going to focus on the return type for a constructor in Java.
在这个快速教程中,我们将重点讨论Java中构造函数的返回类型。
First, we’ll get familiar with how object initialization works in Java and the JVM. Then, we’ll dig deeper to see how object initialization and assignment work under-the-hood.
首先,我们将熟悉对象初始化在Java和JVM中如何工作。然后,我们将深入了解对象初始化和赋值是如何在内部进行的。
2. Instance Initialization
2.实例初始化
Let’s start with an empty class:
让我们从一个空的类开始。
public class Color {}
Here, we’re going to create an instance from this class and assign it to some variable:
在这里,我们要从这个类中创建一个实例,并把它分配给某个变量。
Color color = new Color();
After compiling this simple Java snippet, let’s take a peek at its bytecode via the javap -c command:
编译完这个简单的Java片段后,让我们通过javap -c命令看一下它的字节码。
0: new #7 // class Color
3: dup
4: invokespecial #9 // Method Color."<init>":()V
7: astore_1
When we instantiate an object in Java, the JVM performs the following operations:
当我们在Java中实例化一个对象时,JVM会执行以下操作。
- First, it finds a place in its process space for the new object.
- Then, the JVM performs the system initialization process. In this step, it creates the object in its default state. The new opcode in the bytecode is actually responsible for this step.
- Finally, it initializes the object with the constructor and other initializer blocks. In this case, the invokespecial opcode calls the constructor.
As shown above, the method signature for the default constructor is:
如上所示,默认构造函数的方法签名是。
Method Color."<init>":()V
The <init> is the name of instance initialization methods in the JVM. In this case, the <init> is a function that:
<init> 是JVM中实例初始化方法的名称。在这种情况下,<init>是一个函数,它。
- takes nothing as the input (empty parentheses after the method name)
- returns nothing (V stands for void)
Therefore, the return type of a constructor in Java and JVM is void.
因此,在Java和JVM中,构造函数的返回类型是void。。
Taking another look at our simple assignment:
再看一下我们的简单任务。
Color color = new Color();
Now that we know the constructor returns void, let’s see how the assignment works.
现在我们知道构造函数返回void,让我们看看赋值是如何进行的。
3. How Assignment Works
3.作业如何进行
JVM is a stack-based virtual machine. Each stack consists of stack frames. Put simply, each stack frame corresponds to a method call. In fact, JVM creates frames with a new method call and destroys them as they finish their job:
JVM是一个基于堆栈的虚拟机。每个堆栈由堆栈框架组成。简单地说,每个堆栈帧对应于一个方法调用。事实上,JVM通过一个新的方法调用来创建框架,并在它们完成工作后将其销毁。
Each stack frame uses an array to store local variables and an operand stack to store partial results. Given that, let’s take another look at the bytecode:
每个堆栈框架使用一个数组来存储局部变量,使用一个操作数堆栈来存储部分结果。鉴于此,让我们再看一下字节码。
0: new #7 // class Color
3: dup
4: invokespecial #9 // Method Color."<init>":()V
7: astore_1
Here’s how the assignment works:
以下是任务的运作方式。
- The new instruction creates an instance of Color and pushes its reference onto the operand stack
- The dup opcode duplicates the last item on the operand stack
- The invokespecial takes the duplicated reference and consumes it for initialization. After this, only the original reference remains on the operand stack
- The astore_1 stores the original reference to index 1 of the local variables array. The prefix “a” means that the item to be stored is an object reference, and the “1” is the array index
From now on, the second item (index 1) in the local variables array is a reference to the newly created object. Therefore, we don’t lose the reference, and the assignment actually works — even when the constructor returns nothing!
从现在开始,局部变量数组中的第二个项目(索引1)是对新创建对象的引用。因此,我们不会丢失引用,而且赋值实际上是有效的–即使构造函数没有返回任何东西也是如此。
4. Conclusion
4.总结
In this quick tutorial, we learned how the JVM creates and initializes our class instances. Moreover, we saw how the instance initialization works under-the-hood.
在这个快速教程中,我们学习了JVM如何创建和初始化我们的类实例。此外,我们还看到了实例初始化是如何在内部进行的。
For an even more detailed understanding of the JVM, it’s always a good idea to check out its specification.
要想更详细地了解JVM,查看其规范总是一个好主意。