加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:26:32 所属栏目: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
一个对象可以存储在堆栈而不是堆上吗?

我最近经历了这个博客
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读