使用堆栈空间Java数组 [英] Java array using stack space
问题描述
这是声明的Java数组通常的方式:
This is the usual way for declare a Java array:
int[] arr = new int[100];
但这种阵列使用的堆空间。有没有我们可以使用类似于C ++堆栈空间声明数组的方式?
But this array is using heap space. Is there a way we can declare an array using stack space like c++?
推荐答案
在一句话,没有。
存储在堆栈中的唯一变量是原语和对象引用。在您的例子中,改编
引用存储在堆栈上,但它引用的数据是在堆上。
The only variables that are stored on the stack are primitives and object references. In your example, the arr
reference is stored on the stack, but it references data that is on the heap.
如果你问这个问题从C ++来,因为你要确保你的内存清理,读到的垃圾收集。总之,Java的自动进行清理堆内存以及内存堆栈上的照顾。
If you're asking this question coming from C++ because you want to be sure your memory is cleaned up, read about garbage collection. In short, Java automatically takes care of cleaning up memory in the heap as well as memory on the stack.
这篇关于使用堆栈空间Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!