Java class (basic detailed explanation) memory principle


First of all, I want to talk about what a class is and what it can do.

Class definition

定义:类是构建对象的模板或蓝图,由类构造对象的过程称为创造类的实例。

From this definition, we know that a class is a template for creating objects. That is to say, if we need an object, we need to create the object from a class.

Classes can be divided into user-defined classes and predefined classes. For ease of understanding, we will start with user-defined classes.

Custom class

package com.csdn;

public class ClassTest {
    
    
    //属性
    int a=1;
    //无参构造器
    public ClassTest() {
    
    
    }
    //有参构造器
    public ClassTest(int a) {
    
    
        this.a = a;
    }
    //方法
    public void a(){
    
    
        System.out.println("a()方法被调用");
    }
    
}

From the above code, we can see that a basic class has properties, methods and constructors.

Constructor (key understanding)

构造器是一种特殊的方法用来构造并初始化对象来完成对新对象的初始化

构造器分为无参构造器和有参构造器,构造器名称需要与类名相同

No-argument constructor

访问修饰符 类名(){
    
    
}

Parametric constructor

访问修饰符 类名(int a,int b){
    
    //参数可以有任意个
	this.a = a;
	this.b = b;
}

this keyword

The this keyword appears here. What does this do? Let’s look at the following code first.


public class Person {
    
    
    int a;
    int b;
//无参构造器
    public Person(){
    
    
    }
//有参构造器
    public Person(int A int B){
    
    
        a=A;
        b=B;
    }
    }

When using the parameterized constructor in this code to initialize the object, we assign the value of A to attribute a in the Person class, and assign the value of B to attribute b in the Person class, completing the initialization, which is obviously correct.
But is it okay if we write the parameterized constructor in this form?

public Person(int a int b){
    
    
        a=a;        
        b=b;   
}

This doesn't work because the a's here only represent the passed in actual parameter values ​​rather than the attribute a in the Person class.
So how to solve this problem?
The following code can solve it

public Person(int a int b){
    
    
        this.a=a;
        thia.b=b;
    }

The jvm virtual machine assigns this to each object (assign this the same address as the object) to represent the current object.

The this keyword can distinguish member variables and method parameters: When the parameter name of the method is the same as the member variable name, you can use the this keyword to refer to the member variable. This can clearly specify that the member variables to be operated on are not the method parameters.

Constructor creates and initializes objects

In the following code, we define properties, methods and constructors in a most basic class

public class ClassTest {
    
    
    //属性
    int a=1;
    //无参构造器
    public ClassTest() {
    
    
    }
    //有参构造器
    public ClassTest(int a) {
    
    
        this.a = a;
    }
    //方法
    public void a(){
    
    
        System.out.println("a()方法被调用");
    }
    
}

Next, use the class to create an object

1 Create an object using a no-argument constructor

 Person person = new Person();

At this time, an object named person is created. The attributes and methods of the Person class can be called through the object.
Because the object is constructed through the no-parameter constructor, the attributes need to be assigned manually.

public class Test{
    
    
    public static void main(String[] args) {
    
    
        Person person = new Person();//无参构造器初始化对象
        person.age = 21;//为属性赋值
        person.name = "lz";
        System.out.println(person.age);//调用Person类的age属性
        System.out.println(person.name);
        person.a();//通过对象调用a方法
    }
}

2 Create objects using parameterized constructors

Person person = new Person(21,"lz");

The parameterized constructor will pass the value into the object's properties, which eliminates the step of assigning values ​​​​to the properties compared to the parameterless constructor.

class Test{
    
    
    public static void main(String[] args) {
    
    
        Person person = new Person(21,"lz");//有参构造器初始化对象
        System.out.println(person.age);
        System.out.println(person.name);
        person.a();
    }
}

Predefined classes

预定义类是java已经定义好的类可以直接用来创建对象

For example, the Date class in Java is a class used to represent date and time. It is located in the java.util package.
Two tasks are completed in the following code

1 使用java已经定义好的类创建Date对象来创建表示当前日期和时间的Date对象
2 使用Math类获取PI值
import java.util.Date;

public class Test01 {
    
    
    public static void main(String[] args) {
    
    
        Date date = new Date();//需要引入java.util.Date包
        System.out.println(date);
        System.out.println(Math.PI);//Math类属于java.lang包属于java核心库的一部分不需要额外引用

    }
}

static factory method

You can use the method directly without using a constructor to construct the object. You will call the constructor yourself, such as: localDate date = LocalDate.now(); date represents the current date, and different instances are obtained through different suffixes. We observe this statement, and first
create For the localDate type variable date, it is found that the application type assigned to date is not an object but a method, and is referenced through the class name. method. This is actually a static factory method.

Class creation object process analysis:

1 加载目标类,只会加载一次

2 在堆中分配对象的空间(地址)

 先new出一个对象,然后再调用构造器初始化对象

3 完成对象初始化【1 默认初始化age=0 name=null 2 显示初始化 age=90 name=null 3 构造器初始化 age=16 name=lz】

4 把对象在堆中的地址返回给p

*/
This picture is taken from Han Shunping's Java course. It is a very good Java course that everyone is recommended to learn.

Cat cat=new Cat();
cat.name="小白":
cat,age=12;
cat.color="白色";

Cat cat=new Cat()
The white and white here are strings, so they will be stored in the constant pool in the method area, and the addresses of these two constants will be stored in the heap.
Age 12 is a basic data type, so it will be stored in the heap.

process

创建一个 Cat 对象:
通过 new Cat() 语句在堆中分配一个 Cat 对象的空间。
使用默认构造器或者用户自定义的构造器对 Cat 对象进行初始化。
将对象在堆中的地址返回给栈中的变量 cat。
给 cat 对象设置属性值:

使用 cat.name = "小白" 将对象的 name 属性设置为 "小白"。
使用 cat.age = 12 将对象的 age 属性设置为 12。
使用 cat.color = "白色" 将对象的 color 属性设置为 "白色"

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_64618264/article/details/132889732