Java变量放置在堆栈或堆上 [英] Java variable placed on stack or heap

查看:86
本文介绍了Java变量放置在堆栈或堆上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java不太了解.

I don't have much idea on Java.

我正在浏览几个链接,发现博客上写着"Java Primitives存储在堆栈上",我认为这取决于实例变量或局部变量.

I was going through few links and found blog says "Java Primitives stored on stack", which I feel it depends on instance variable or local variable.

经过几个链接后,我的结论是,

After going through several links my conclusion is,

类变量(基元)作为对象包含的对象的一部分存储在堆中.

Class variables – primitives – are stored on heap as a part of Object which it contains.

类变量– object(用户定义)–作为其包含的Object的一部分存储在堆中.对于参考对象和实际对象都是如此.

Class variables – object(User Defined) – are stored on heap as a part of Object which it contains. This is true for both reference and actual object.

方法变量-基元-作为该堆栈框架的一部分存储在堆栈中.

Method Variables – Primitives – are stored on stack as a part of that stack frame.

方法变量–对象(用户定义)–存储在堆上,但对堆上该区域的引用作为该堆栈框架的一部分存储在堆栈上. 如果对象中包含另一个对象,则引用也可以存储在堆中.

Method Variables – object(User Defined) – are stored on heap but a reference to that area on heap is stored on stack as a part of that stack frame. References can also be get stored on heap if Object contains another object in it.

静态方法(实际上是所有方法)以及静态变量都存储在堆中.

Static methods (in fact all methods) as well as static variables are stored in heap.

如果我的理解是错误的,请纠正我.谢谢.

Please correct me if my understanding is wrong. Thanks.

推荐答案

JVM中有一些优化甚至可以使用对象堆栈,这可以减少垃圾收集工作.

There are some optimizations in the JVM that may even use the Stack for Objects, this reduces the garbage collection effort.

类存储在堆的特殊部分,但这取决于您使用的JVM. (Permgen f.e.位于Hotspot< = 24).

Classes are stored on a special part of the heap, but that depends on the JVM you use. (Permgen f.e. in Hotspot <= 24).

通常,您不必考虑数据的存储位置,而应更多地考虑诸如可见性和语义寿命之类的语义.到目前为止,您对问题的解释都很好.

In general you should not have to think about where the data is stored, but more about the semantics like visibility and how long something lives. Your explanation in the questions looks good so far.

这篇关于Java变量放置在堆栈或堆上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆