Is a Java array of primitives stored in stack or heap? -


i have array declaration this:

int a[]; 

here a array of primitive int type. array stored? stored on heap or stack? primitve type int, primitive types not stored on heap.

as gurukulki said, it's stored on heap. however, post suggested misunderstanding due well-intentioned person propagating myth "primitives live on stack". untrue. local variables have values on stack, not primitive variables local...

for example, consider this:

public class foo {     int value; } ...  public void someothermethod() {     foo f = new foo();     ... } 

now, f.value live? myth suggest it's on stack - it's part of new foo object, , lives on heap1. (note value of f reference, , lives on stack.)

from there, it's easy step arrays. can think of array being lot of variables - new int[3] bit having class of form:

public class arrayint3 {     public readonly int length = 3;     public int value0;     public int value1;     public int value2; } 

1 in fact, it's more complicated this. stack/heap distinction implementation detail - believe jvms, possibly experimental ones, can tell when object never "escapes" method, , may allocate whole object on stack. however, it's conceptually on heap, if choose care.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -