一个对象可以存储在堆栈而不是java中的堆中吗?

前端之家收集整理的这篇文章主要介绍了一个对象可以存储在堆栈而不是java中的堆中吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一个对象可以存储在堆栈而不是堆上吗?

我最近经历了这个博客
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

Can an object be stored on the stack instead of the heap?

Yes,an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack,and not on the heap. Suppose we have a C++ class called Member,for which we want to create an object. We also have a function called somefunction( ). Here is what the code would look like:

他们建议对象可以存储在堆中.这是令人困惑的.

我以为,

> Java中的所有对象都存储在堆上.
无论是由…创建
一个.新关键字
湾使用Class.forName().
C.使用clone().
天.使用newInstance()方法
即使用对象反序列化
>方法,线程和变量都是堆栈.
>类变量(静态变量)作为与该类关联的Class对象的一部分存储.此类对象只能由JVM创建并存储在永久生成中.

如果我错了,请做正确的我

现在我怀疑对象是否可以以任何形式存放在堆栈中.

谢谢.

解决方法

所有类实例都存储在堆中.

这里有一些可信的来源:
从JVM规范:
https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf

2.5.3 Heap

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area
from which memory for all class instances and arrays is allocated.

来自Java语言规范:https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf

17.4.1 Shared Variables

Memory that can be shared between threads is called shared memory or heap memory. All instance fields,static fields,and array elements are stored in heap memory.

猜你在找的Java相关文章