In Java, memory comprised of stack and heap.
stack: local variables 当前执行状态
heap: dynamic elements, such as new object 动态分配(程序在运行时分配的内存)的object(new出来的)
stack heap
Jack -> name = "Jack"
primitive defined locally (such as within methods) goes to stack.
primitive defined as a part of an object goes to heap.
static variable goes to heap.
- Occupies consecutive memory
- Can access elements via index in O(1) time
int[] array;
// Valid
array = new int[] {1,2,3};
// Invalid
array = {1,2,3};
// Valid
int[][] 2darray = new int[5][]
// Invalid
int[][] 2darray = new int[][]
stack heap
int[] array -> {0,0,0} length=3
point to
