12

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 an array using stack space like c++?

icn
  • 17,126
  • 39
  • 105
  • 141
  • 1
    @JigarJoshi: moar stackoverflow. – tskuzzy Jun 08 '12 at 18:03
  • Array is a container object. All objects go on the heap. – jn1kk Jun 08 '12 at 18:03
  • 1
    @JigarJoshi I think is it faster to access space in stack – icn Jun 08 '12 at 18:04
  • possible duplicate of [Java Array is stored in stack or heap?](http://stackoverflow.com/questions/2099695/java-array-is-stored-in-stack-or-heap) –  Jun 08 '12 at 18:05
  • 6
    You lose a micro-second or two in creating/collecting off the heap, but, unless you are doing this skillions of times, worrying about such optimizations is futile. – user949300 Jun 08 '12 at 18:07

4 Answers4

21

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.
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
12

In a word, no.

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.

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.

cheeken
  • 33,663
  • 4
  • 35
  • 42
  • If the JIT sees an opportunity to optimize by keeping the array in a particular place in memory, it might do that, but that's its job, not yours. – Louis Wasserman Jun 08 '12 at 18:28
3

Arrays are dynamically allocated so they go on the heap.

I mean, what happens when you do this:

int[] arr = new int[4];
arr = new int[5];

If the first allocation was done on the stack, how would we garbage collect it? The reference arr is stored on the stack, but the actual array of data must be on the heap.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
1

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.

the8472
  • 40,999
  • 5
  • 70
  • 122