Three kinds of initialization and memory analysis of Java arrays-10 days study notes

Java memory

heap

  • Store new objects and arrays.
  • It can be shared by all threads and will not be referenced by other objects.

Stack

  • Store the basic variable inner type (also contains the basic value of this inner type)
  • Reference to the variable of the object (the specific value stored in this heap will be referenced)
  • The heap is on the stack

Method area

  • Can be shared by all threads
  • Contains all static and class

Array

  • The array does not exist when it is declared, only when an array is created.
  • When removing the array, the total number of groups created cannot be exceeded,
    but we generally write the declaration and creation together

Three kinds of initialization

Static initialization

//一维数组的静态初始化
int[] a = {1,2,3,4,5,6,7,8,9} ;
//二维数组的静态初始化
int[] []  b ={
   
   {1,2},{3,4},{5,6}}
//创建加赋值

//引用类型先了解Mam[] mans = {new mans(1,2),new mans(3,4)};

Dynamic initialization

  • Dynamic initialization includes default initialization.
//一维数组
int[] nums = int[3];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
//二维数组
int[][] nums1 = int [4][2];

nums1[0][1] = 1;
nums2[0][2] = 2;

Default initialization of the array

Except for the 8 basic types, all others are reference types.
Arrays are reference types. Its elements are equivalent to instance variables of the class. Therefore, as soon as the array is created, each element in it is implicitly initialized like an instance variable.

Guess you like

Origin blog.csdn.net/yibai_/article/details/114603824