Java 原语是在栈上还是堆上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3646632/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:07:35  来源:igfitidea点击:

Do Java primitives go on the Stack or the Heap?

javastackheap

提问by Tom Brito

I just know that the non-primitives (the objects) go on the heap, and methods go on the stack, but what about the primitive variables?

我只知道非基元(对象)在堆上,方法在栈上,但是基元变量呢?

--update

- 更新

Based on the answers, I could say the heap can have a new stack and heap for a given object? Given that the object will have primitive and reference variables..?

根据答案,我可以说堆可以有一个新的堆栈和给定对象的堆?鉴于该对象将具有原始变量和引用变量..?

采纳答案by Hyman Edmonds

Primitives defined locally would be on the stack. However if a primitive were defined as part of an instance of an object, that primitive would be on the heap.

本地定义的原语将在堆栈上。但是,如果将原语定义为对象实例的一部分,则该原语将位于堆上。

public class Test {
    private static class HeapClass {
        public int y; // When an instance of HeapClass is allocated, this will be on the heap.
    }
    public static void main(String[] args) {
        int x=1; // This is on the stack.
    }
}

With regards to the update:

关于更新:

Objects do not have their own stack. In my example, int ywould actually be part of each instance of HeapClass. Whenever an instance of HeapClass is allocated (e.g. new Test.HeapClass()), all member variables of HeapClass are added to the heap. Thus, since instances of HeapClassare being allocated on the heap, int ywould be on the heap as part of an instance of HeapClass.

对象没有自己的堆栈。在我的示例中,int y实际上是HeapClass. 每当分配 HeapClass 的实例时(例如new Test.HeapClass()),HeapClass 的所有成员变量都会添加到堆中。因此,由于 的实例HeapClass在堆上分配,因此int y将作为 实例的一部分在堆上HeapClass

However, all primitive variables declared in the body of any method would be on the stack.

但是,在任何方法的主体中声明的所有原始变量都将在堆栈上

As you can see in the above example, int xis on the stack because it is declared in a method body--not as a member of a class.

正如你在上面的例子中看到的,int x在堆栈上是因为它是在方法体中声明的——而不是作为类的成员。

回答by Tom Hawtin - tackline

All local variables (including method arguments) go on the stack; objects and all their fields are stored in the heap. Variables are always primitives or referencesto objects.

所有局部变量(包括方法参数)都进入堆栈;对象及其所有字段都存储在堆中。变量始终是原语或对对象的引用

Java implementations may actually store objects on the heap in such a way that it still complies with the specification. Similarly local variables may be stored in registers or become indistinct through optimisation.

Java 实现实际上可以将对象存储在堆上,这样它仍然符合规范。类似地,局部变量可以存储在寄存器中或通过优化变得模糊。

回答by Logan Capaldo

primitives can be found in both places.

在这两个地方都可以找到原语。

class Foo
{
   public int x;
   public static void Main()
   {
      int y = 3; // y is on the stack
      Foo f = new Foo();  // f.x is probably on the heap
   } 
}

except you shouldn't really care unless you're building a JVM. A really clever optimizer might decide that since the Foo that f points to never escapes Main, and is never passed to another function that it is safe to allocate it on the stack.

除非你真的不应该关心,除非你正在构建一个 JVM。一个真正聪明的优化器可能会决定,由于 f 指向的 Foo 永远不会转义 Main,并且永远不会传递给另一个函数,因此在堆栈上分配它是安全的。

With regards to the update:

关于更新:

The stack and the heap aren't distinguished by what is stored in them, but rather the operations provided for them. The stack allows you to allocate a piece of memory in a LIFO fashion, you can't deallocate a piece until all the pieces younger than it have also been deallocated. This conveniently aligns with how a call stack is used. You can put anything on the stack as long as it is ok for that thing to go away when your function returns. This is an optimization, as it is very quick to allocate and deallocate from a stack since it only supports being used in this manner. One could store all the local variables for a function on the heap in an implementation if one wanted to. The heap is more flexible, and consequently more expensive to use. It would not be accurate to say that an object has a stack and a heap, as I said, what distinguishes the stack from the heap is not what is in it, but the available operations.

堆栈和堆的区别不是存储在它们中的内容,而是为它们提供的操作。堆栈允许您以 LIFO 方式分配一块内存,直到所有比它年轻的块也都被释放后,才能释放一块内存。这很方便地与调用堆栈的使用方式保持一致。你可以把任何东西放在堆栈上,只要当你的函数返回时它可以消失。这是一种优化,因为从堆栈中分配和解除分配非常快,因为它只支持以这种方式使用。如果需要,可以在实现中将函数的所有局部变量存储在堆上。堆更灵活,因此使用起来更昂贵。正如我所说,说一个对象有一个栈和一个堆是不准确的,

回答by Mark Cidade

Primitive values are allocated on the stack unless they are fields of an object, in which case they go on the heap. The stack is used for evaluation and execution, so no it doesn't make sense to say that objects with primitive fields have a stack—it is still considered to be part of the heap. Even Stackobjects are allocated on the heap.

原始值在堆栈上分配,除非它们是对象的字段,在这种情况下它们会在堆上。堆栈用于评估和执行,因此说具有原始字段的对象具有堆栈是没有意义的——它仍然被认为是堆的一部分。甚至Stack对象也分配在堆上。