Why Java member variables can not be initialized and local variables must be initialized

  I have always been a little confused about the memory of the java virtual machine. Today, I took out the in-depth understanding of the jvm virtual machine and looked over it, and managed the java memory area.

  The ava virtual machine stack describes the memory model of java method execution: each method will create a stack frame to store the local variable table, operand stack, dynamic link, method exit and other information when it is executed. The memory space required by the local variable table is allocated during compilation. When entering a method, how much local variable space this method needs to allocate in the stack frame is completely determined, and the size of the local variable table will not be changed during the method execution. This sentence shows that local variables must be initialized when they are created to determine the size of the allocated memory.

  The java heap stores instances of all objects, and almost all object instances allocate memory here. In the hotspot virtual machine, after memory allocation, the virtual machine needs to initialize the allocated memory space to a zero value. This step ensures that the instance fields of the object can be used directly in the java code without assigning initial values, and the program can access the zero values ​​corresponding to the data types of these fields.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471835&siteId=291194637