This is the 22nd day of my participation in Gwen Challenge

Java execution process

A class is the most basic unit in object-oriented programming.

A Class in Java contains three things:

attribute

  • Attributes are also called member variables.

  • Properties are used to define the data of a class or class object (static characteristics).

  • The scope is the entire class body.

methods

  • Method is used to define the behavior characteristics of a class or class object (performing actions) (dynamic).

  • A method is similar to a process-oriented function, which is the most basic unit.

  • In object orientation, the most basic unit is a class, and methods are subordinate to classes and objects.

A constructor

  • The structural methods are divided into non-parametric structural methods. Parameterized constructor.

  • The constructor must be consistent with the class name.

  • If no constructor is set, the system automatically generates a no-parameter constructor.

Attribute definition format:

Attribute type Attribute name = [default];Copy the code

Method definition format:

[modifier] method return value type Method name (parameter){statement}Copy the code

The following code is used for example analysis

public class Person{
	public String name;   // Attributes with modifiers
	int age;
	public void show(a){  / / method
		System.out.println("Name:"+name+", age:+age);
	}
	/* Person(){// Person(){// Person(){// Person(){// Person(){// Person(){// Person(){// Person(){// Person()
	/* / Person(String name, int age){ this.name = name; this.age = age; } * /
	public static void main (String[] args){
		// Call the constructor with the manual arguments above
		Person p1 = new Person("Bill".18);
		// call the no-argument constructor
		Person p2 = new Person();
		// The no-argument constructor requires the following assignment
		p2.name = "Zhang"; p2.age =18; p2.show(); }}Copy the code

Memory analysis

Now that we’ve covered the basics of classes, let’s do memory analysis based on the code above.

The JVM memory model is complex, and to better understand object-oriented and object-oriented code, let’s simplify the model. (This memory analysis is not intended to understand the JVM memory model)

The characteristics of the above three memory models are introduced below:

The stack

Stacks describe the in-memory model of method execution, and a stack frame is created for each method called.

  • The JVM creates a virtual machine stack for each thread.

  • The stack is private and is not shared between threads.

  • A stack is a contiguous block of storage space

The heap

The heap is used to store created objects.

  • The JVM has only one heap.

  • The heap is shared by all threads.

  • A heap is a discontinuous space with flexible allocation.

Methods area

The method area is actually a heap that stores classes, constants (static keywords), and other related information.

  • The JVM has only one method area.

  • Can be Shared.

  • Store unchanging information.

Operation process

  • Each program uses the main method as its entry point and executes the second statement (the first is ignored for now).
Person p2 = new Person();
Copy the code
  1. At this point, a new stack frame will be opened in the stack, the main method stack frame;

  2. The method area loads the entire class template and stores information about the class.

  3. An object of a new class is created at the heap.

Java uses the new keyword to call the constructor, which returns an instance (object) of the class. But the object is not created entirely by the constructor.

There are 4 steps to creating a method:

Allocates object space in the heap and initializes the object’s member variables explicitly (such as int I = 3). The constructor returns the object address.

When the Person() constructor call completes, the stack continues to execute the next statement in main. As shown in figure

The next statement is

p2.name = "Zhang"; p2.age =18;
Copy the code

This assigns name to the method field;

Let’s move on to the next statement

p2.show();
Copy the code

  • The show method is executed, and we notice that the show method has age and name. So whose name and age are they? We can look at p2.show().

  • P2 has an address of 0x11 and refers to the 0x11 object in the heap, so the name and age in the show method are known.

  • So when the method is finished executing, the show method is off the stack, and the main method is finished executing, and it needs to be off the stack, and when the main method is finished executing, the class information in the object and method area is cleared. The memory will go back to its original state.