Array in java
Array definition: a set of data of the same type, such as int, String, or a class. You can simply regard an array as a container, however, the data stored in this container is of the same type,
Array format:
Element type [] array name = new element type [number of elements or array length]
Instance code:
int[] a = new int[3];System.out.println(a[2]);
There is also a format: specifically, the elements in the array are written.
int[] a = new int[]{1,2,3,4,5,6};You can also write this statement:int[] a = {1,2,3,4,5,6};
The int array is defined. The length of the array is 3. When I print the lower three data, it is 0. I'm curious. I will think about it later, because each int value in the int array is initialized with the default value, it is 0. If we want to assign a value to it, do this.A [0] = 2;
A [1] = 4;
A [2] = 3;
This is to assign values to each of the arrays,
So how does it correspond in the memory? In java JVM, there are usually stack memory and heap memory. So what data does each of them store?
Stack memory:
1: stores local variables. Once the local variables are in the final scope, the variables are automatically released in the stack memory, saving my memory. This is the internal optimization done by the JVM, therefore, stack memory updates are fast.
2: The local variable does not have a default initialization value. You must give it a default value.
Heap memory:
1: stores arrays, sets, and objects
2: all new items are in heap memory.
3: The value in heap memory has a default initialization value, which varies depending on the type of initialization value.
4: when an object is null in memory, the garbage collector in java will automatically recycle the non-pointing object at a certain time, thus saving memory space.