堆栈或堆上的 Java 原始数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22779145/
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 Primitive data type on Stack or Heap?
提问by dev_marshell08
If i am correct Primitive data type defined locally would be on the stack. However if a primitive data type were defined as part of an instance of on object that primitive would go on heap.
如果我是正确的,本地定义的原始数据类型将在堆栈上。但是,如果原始数据类型被定义为 on 对象实例的一部分,那么该原始数据类型将进入堆。
class Test
{
int y=10; // defined as part of the class
public void function1(){
int x = 5; // defined locally
}
public static void main(String[] args)
{
Test obj = new Test();
}
}
So in the above code will x be stored on the stack and y on the heap ? I am confused how they are stored and why does it matter stack or heap ?
那么在上面的代码中,x 会存储在堆栈中,而 y 会存储在堆中吗?我很困惑它们是如何存储的,为什么堆栈或堆很重要?
采纳答案by Dawood ibn Kareem
When a method is called, certain data is placed on the stack. When the method finishes, data is removed from the stack. At other points in a program's execution, data is added to the stack, or removed from it.
当一个方法被调用时,某些数据被放置在堆栈上。当该方法完成时,数据从堆栈中移除。在程序执行的其他点,数据被添加到堆栈中,或从堆栈中删除。
Therefore, if you have a variable which is intended to outlive the execution of the method that created it, it needs to be on the heap. This applies both to any objects that you create, and any primitives that are stored within those objects.
因此,如果您有一个旨在比创建它的方法的执行时间更长的变量,则它需要在堆上。这既适用于您创建的任何对象,也适用于存储在这些对象中的任何基元。
However, if a variable is intended to go out of scope shortly after its creation - say, at the end of the method in which it's created, or even earlier, then it's appropriate for that variable to be created on the stack. Local variables and method arguments fit this criterion; if they are primitives, the actual value will be on the stack, and if they are objects, a reference to the object (but not the object itself) will be on the stack.
但是,如果一个变量打算在创建后不久离开作用域——比如说,在创建它的方法结束时,甚至更早,那么在堆栈上创建该变量是合适的。局部变量和方法参数符合这个标准;如果它们是基元,实际值将在堆栈中,如果它们是对象,则对对象的引用(但不是对象本身)将在堆栈中。
In your specific example, x
is unusable as soon as function1
finishes running. So it's reasonable for it to be created on the stack. At the end of function1
, data is effectively removed from the stack, including x
. On the other hand, the variable y
is expected to still exist for as long as the containing object exists; if it were created on the stack, it would cease to exist once the object constructor which created it finishes running. Therefore y
must be created on the heap.
在您的特定示例中,x
一旦function1
完成运行就无法使用。所以它在堆栈上创建是合理的。在 结束时function1
,数据被有效地从堆栈中删除,包括x
。另一方面,y
只要包含对象存在,变量就会一直存在;如果它是在堆栈上创建的,那么一旦创建它的对象构造函数完成运行,它就会停止存在。因此y
必须在堆上创建。
回答by TheLostMind
Instance level variables(can also be primitives) are part of instances (objects). So, they will be allocated on the heap (as part of the object.)
实例级变量(也可以是基元)是实例(对象)的一部分。因此,它们将在堆上分配(作为对象的一部分。)
Method level / Local variables : Stack (for Primitives) or Heap(Objects with references on the Stack). Strings can be allocated either on Stack or on heap.
方法级别/局部变量:堆栈(对于基元)或堆(在堆栈上有引用的对象)。字符串可以在堆栈或堆上分配。
回答by Prakash Bhagat
Only local primitive variablesand references to object(i.e. variable declared in method) are stored in stack. Others are stored in heap
只有局部原始变量和对对象的引用(即在方法中声明的变量)存储在堆栈中。其他存储在堆中
回答by Weibo Li
When you initiate a Test
instance, all of it's fields such as y
will be stored in heap.
当您启动一个Test
实例时,它的所有字段y
都将存储在堆中。
When you call function1
, the local varaible x
will be push into the stack.
当您调用 时function1
,本地变量x
将被压入堆栈。
But in your example, x
is not stored in stack because function1
is never called.
但是在您的示例中,x
未存储在堆栈中,因为function1
从未调用过。
回答by Eugene
I am pretty sure you got your answer by now... one thing to notice. Even if you variables will be stored on the heap, Java will still load their values on the stack when it needs to act upon them. That is the way JVM works - it is a stack language.
我很确定你现在已经得到了答案......需要注意一件事。即使您的变量将存储在堆中,Java 仍会在需要对它们进行操作时将它们的值加载到堆栈中。这就是 JVM 的工作方式——它是一种堆栈语言。