Memory map when method is called in java

Memory map when method is called in java

First of all, we must understand the memory map in java:
Insert picture description here

The first method written in the class is:

/**创建一个学生的类,
*/
public class Student {
    
    
	
	String name;
	int age;
	
	public void study() {
    
    
		System.out.println("study");
	}
	
	public void talk() {
    
    
		
		System.out.println("talk");
	}

}

After the class file is generated, the class file is read to the method area through the class loader, and the storage format is as follows.
Insert picture description here

In the test program, call the student class.

public class TestStu {
    
    
	
	public static void main(String[] args) {
    
    
		
		Student stu = new Student();
		
		stu.name = "Tom";
		
		stu.age = 18;
		
		stu.study();
		
		stu.talk();
		
	}

}

When the main method in the test program is executed, first main opens up a memory space in the stack, and when the program is executed

Student stu = new Student();

The memory of stu will be allocated in the stack and stored in the address of the memory space opened in the heap. The memory space opened in the heap is the template development in the method area, and the memory of common attributes is opened.
Insert picture description here
When the main method is executed

stu.study()

The study method starts to stack (first in last out, last in first out, execute the element at the top of the stack),

The methods exist in the form of signatures, and memory space is allocated only when they are called.

Insert picture description here
When the study method call is complete, pop out of the stack and execute

stu.talk();

Insert picture description here
Push the stack by the method signature of talk in the method area
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38705144/article/details/109496843
Recommended