初始化的java数组是进入堆栈还是堆? [英] Does initialized java array go onto stack or heap?

查看:113
本文介绍了初始化的java数组是进入堆栈还是堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void someMethod() {
  byte[] array = { 0, 0 };
}

这个数组会存储在堆中还是堆栈中?

Will this array be stored in heap or on the stack?

推荐答案

你可以认为它总是在堆上。

我相信一些智能虚拟机能够堆栈分配对象,如果它们可以检测到它是安全的 - 但从概念上来说它就在堆上。特别是,所有数组类型都是引用类型(即使元素类型是原始类型),因此数组变量(位于堆栈上)只是对对象的引用,对象通常会堆在堆上。

I believe some smart VMs are able to stack-allocate objects if they can detect it's safe - but conceptually it's on the heap. In particular, all array types are reference types (even if the element type is primitive), so the array variable (which is on the stack) is just a reference to an object, and objects normally go on the heap.

特别想象一下小改动:

byte[] someMethod() { 
    byte[] array = { 0, 0 };
    return array;
}

如果数组是在堆栈上分配的,那么返回的引用必须包含什么参考?

If the array were allocated on the stack, what would the returned reference have to refer to?

这篇关于初始化的java数组是进入堆栈还是堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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