Java array using stack space

前端 未结 4 1152
心在旅途
心在旅途 2020-12-11 17:15

This is the usual way for declare a Java array:

int[] arr = new int[100];

But this array is using heap space. Is there a way we can declare

4条回答
  •  -上瘾入骨i
    2020-12-11 17:55

    It's not yet supported as a language feature, because that would require value types since passing on-stack data by reference would not be safe.

    But as an optimization (escape analysis) the JVM may already do that for local variables containing small, fixed-size arrays iff it can prove that it does not escape the local/callee scope. That said, it's just a runtime optimization and not some spec guarantee, so relying on it is difficult.

提交回复
热议问题