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
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.