在 Java 中创建对象时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17058499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:49:56  来源:igfitidea点击:

What occurs when object is created in Java?

javaobjectmemory

提问by vuquanghoang

My teacher gave me a question:

老师给了我一个问题:

"What occurs when objects are created in Java".

“在 Java 中创建对象时会发生什么”。

To the best of my knowledge, memory allocation, variable initialization and constructor method invocation happen when an object is created.

据我所知,内存分配、变量初始化和构造函数调用在创建对象时发生。

But my teacher said that I was almost right. The 2 later things are right, except memory heap. Instead, he said the memory allocation occurs. I think that object is stored in heap, so my teacher is wrong. Do you think so?

但是我的老师说我几乎是对的。除了内存堆之外,后面的 2 件事都是正确的。相反,他说内存分配发生了。我认为该对象存储在堆中,所以我的老师错了。你是这样认为的吗?

回答by Oak

As always, the best place to find a solution for these kinds of questions is in the Java Language Specification.

与往常一样,为此类问题找到解决方案的最佳位置是 Java 语言规范。

Specifically, from the section on new instance creation, it can be understood that this is the sequence when a new object is created, as long as no exceptions occur:

具体来说,从新建实例部分可以理解,这是新建对象时的顺序,只要不出现异常:

  1. Memory is allocated.
  2. Fields are initialized to their default values.
  3. The "first line" of the chosen constructor is invoked, unless it's an Object. By first line I mean either explicit call to super()or this(), or an implicit call to super().
  4. The instance initializeris executed and the fields are initialized to their requested values(actually field initialization is usually compiled as an inline part of the instance initializer).
  5. The rest of the constructor code is executed.
  1. 内存已分配。
  2. 字段被初始化为其默认值
  3. 调用所选构造函数的“第一行”,除非它是Object. 第一行我的意思是对super()或 的显式调用this(),或对 的隐式调用super()
  4. 实例初始化被执行,字段被初始化为它们的请求的值(实际字段初始化通常编译为实例初始化的内嵌部分)。
  5. 其余的构造函数代码被执行。

Now, it is possible that your teacher is talking about memory allocation as an actual operating system call - and in that case he's right in the sense that the JVM manages its own heap and thus a Java memory allocation does not necessarily translate to an OS memory allocation call (although it may).

现在,您的老师可能正在将内存分配作为实际的操作系统调用进行讨论 - 在这种情况下,他是对的,即 JVM 管理自己的堆,因此 Java 内存分配不一定转换为操作系统内存分配调用(尽管可能)。

回答by Maroun

I'll answer that using a simple example.

我会用一个简单的例子来回答这个问题。

Say you have a class Car. Now you write:

假设你有一堂课Car。现在你写:

Car car;
car = new Car();

The first statement creates a referencewith carin the stack.

第一条语句创建一个参考car堆栈

In the second statement, the Carclass will be loaded to the main memory, then it will allocate memory for the members of Carin the heap. When this happens, the members will be initialized with values provided by the JVM.

在第二条语句中,Car类将被加载到主内存中,然后它会为Car堆中的成员分配内存。发生这种情况时,成员将使用 JVM 提供的值进行初始化。

回答by Jay Gajjar

While the JVM is running the program, whenever a new object is created, the JVM reserves as portion of the Heap for that object (where the object will be stored). The amount of Heap that gets reserved is based on the size of the object.

当 JVM 运行程序时,无论何时创建新对象,JVM 都会为该对象(将存储对象的位置)保留为堆的一部分。保留的 Heap 数量取决于对象的大小。

The JVM maps out this segment in the Heap to represent all of the attributes of the object being stored. A reference (address in Heap) to the object is kept by the JVM and stored in a table that allows the JVM to keep track of all the objects that have been allocated on the Heap. The JVM uses these references to access the objects later (when the program accesses the object).

JVM 在堆中映射出这个段来表示被存储对象的所有属性。对对象的引用(堆中的地址)由 JVM 保存并存储在一个表中,该表允许 JVM 跟踪已在堆上分配的所有对象。JVM 稍后使用这些引用访问对象(当程序访问对象时)。

回答by selig

On top of what other people have said, if this is the first use of the object then its Class must be initialised -as described in the JLS(the section before the one on new instance creation!).

除了其他人所说的,如果这是第一次使用该对象,那么它的 Class 必须被初始化——如JLS中所述(新实例创建之前的部分!)。

This basically involves loading into memory the necessary information about the class i.e. creating a Klassobject for the static variables and method table to live. This may also involve loading super classes and interfaces. This is all carried out by the ClassLoader.

这基本上涉及将有关类的必要信息加载到内存中,即为Klass静态变量和方法表创建一个对象。这也可能涉及加载超类和接口。这一切都由ClassLoader.

回答by Vivek Ranjan

When object is created in java then these 6 step will be happens one by one--- 1.JVM allocates 8 bytes of memory for the reference variable & assign default value as null.

当在java中创建对象时,这6个步骤将一一发生--- 1.JVM为引用变量分配8个字节的内存并将默认值分配为null。

  1. JVM will verify whether class loading is done or not,if class is already loaded then it will ignore or else it will perform class loading.
  2. At the time of class loading ,if any static variable are there then it will allocating memory.
  3. By using new operator,object memory will e created inside heap memory.
  4. At the time of object creation,if any instance variables are there then those will allocate memory inside object Memory.
  5. It will assign object memory address to the reference variable which is created first.
  1. JVM 会验证类加载是否完成,如果类已经加载,则忽略,否则将执行类加载。
  2. 在类加载时,如果有任何静态变量,那么它将分配内存。
  3. 通过使用 new 运算符,将在堆内存中创建对象内存。
  4. 在创建对象时,如果有任何实例变量,那么它们将在对象内存中分配内存。
  5. 它将对象内存地址分配给首先创建的引用变量。