放置在堆栈或堆上的 Java 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19402207/
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
Java variable placed on stack or heap
提问by Jayesh
I don't have much idea on Java.
我对Java没有太多想法。
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.
我浏览了几个链接,发现博客上写着“Java Primitives storage on stack”,我觉得这取决于实例变量或局部变量。
After going through several links my conclusion is,
经过几个链接后,我的结论是,
Class variables – primitives – are stored on heap as a part of Object which it contains.
类变量——原语——作为它包含的对象的一部分存储在堆上。
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.
方法变量 - 对象(用户定义) - 存储在堆上,但对堆上该区域的引用作为该堆栈帧的一部分存储在堆栈中。如果 Object 中包含另一个对象,则引用也可以存储在堆中。
Static methods (in fact all methods) as well as static variables are stored in heap.
静态方法(实际上是所有方法)以及静态变量都存储在堆中。
Please correct me if my understanding is wrong. Thanks.
如果我的理解有误,请指正。谢谢。
采纳答案by Thomas
There are some optimizations in the JVM that may even use the Stack for Objects, this reduces the garbage collection effort.
JVM 中有一些优化,甚至可以将堆栈用于对象,这减少了垃圾收集工作。
Classes are stored on a special part of the heap, but that depends on the JVM you use. (Permgen f.e. in Hotspot <= 24).
类存储在堆的特殊部分,但这取决于您使用的 JVM。(热点中的 Permgen fe <= 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.
一般来说,您不必考虑数据的存储位置,而应该更多地考虑诸如可见性和某些东西的生命周期之类的语义。到目前为止,您在问题中的解释看起来不错。
回答by Dev
Object are stored in the Heap.
对象存储在堆中。
The object reference stored in the stack.
存储在堆栈中的对象引用。
Static variable stored in the method area.
静态变量存放在方法区。
Example abc obj=new abc();
示例 abc obj=new abc();
abc object save in the heap and the object reference is stored in the stack.
abc 对象保存在堆中,对象引用保存在堆栈中。
static int i=10;
静态 int i=10;
i variable stored in the method area.
i 变量存储在方法区中。
回答by Alexei Kaigorodov
"Method Variables – object(User Defined) – are stored on heap but ..."
“方法变量——对象(用户定义)——存储在堆上,但......”
Wrong. First, method variables are called local variables.
错误的。首先,方法变量被称为局部变量。
Let's consider
让我们考虑一下
public static void main(String[] args) {
List<Integer> model = new ArrayList<Integer>();
Variable model
is placed in the stack frame, not on heap. The referenced object generated with new ArrayList<Integer>()
is placed in the heap but it is not a local variable.
变量model
放置在堆栈帧中,而不是堆中。生成的引用对象new ArrayList<Integer>()
放在堆中,但它不是局部变量。
The 3 things:
3件事:
- variable
model
- generated object
- reference to that object, stored in a variable
- 多变的
model
- 生成对象
- 对该对象的引用,存储在变量中
are quite different, do not mess them up.
是完全不同的,不要把它们搞砸。