Java array using stack space

前端 未结 4 1153
心在旅途
心在旅途 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 18:03

    Arrays are objects irrespective of whether it holds primitive type or object type, so like any other object its allocated space on the heap.

    But then from Java 6u23 version, Escape Analysis came into existence, which is by default activated in Java 7.

    Escape Analysis is about the scope of the object, when an object is defined inside a method scope rather than a class scope, then the JVM knows that this object cant escape this limited method scope, and applies various optimization on it.. like Constant folding, etc

    Then it can also allocate the object which is defined in the method scope,
    on the Thread's Stack, which is accessing the method.
    

提交回复
热议问题