Java object oriented and memory analysis

Posted by Aérolithe on Sat, 11 Dec 2021 13:52:23 +0100

1 object oriented and memory analysis

1.1 object oriented three stages

1.1. 1 object oriented analysis (OOA)

Find and extract commonness from individuality, including nouns used to describe static characteristics and verbs used to describe dynamic characteristics.

1.1. 2 object oriented design (OOD)

The static and dynamic characteristics that the design class should have, namely:
① a noun used to describe static characteristics as "class properties";
② verbs used to describe dynamic characteristics are regarded as "class methods".

1.1. 3 object oriented programming (OOP)

It is described in Java programming language and coded.
Coding - the process of converting information from one form or format to another, also known as the code of computer programming language.

Class 1.2 - [basic template, template with general attributes and functions]

class className{
    //property defining
    [Modifier ]  Property type property name = [Default value] ;
    
//method defining
    [Modifier ]  Method return value type method name(parameter list ) {
              // n statements
    }
}

1.3 create object - [single individual with specific attribute value and specific function]

className  objectName=new className([params...]);

The "new" keyword will automatically call the constructor of the class and return a reference (address) to the newly created object
Constructor [the corresponding method name of the constructor must be the same as the class name]

It is equivalent to the tool of "attribute assignment" for a single produced "object" according to the class template;
- The function of calling the constructor is not to create an instance of the class, because before entering the constructor method body, this(That is, an instance of a class)Already exists;
- The function of call constructor: it is only for the objects of pre created classes,Perform attribute assignment.

//constructor template
[Modifier ] className(params){
	super();
	//toDo
}

1.4 process of creating objects

1.4.1 Part-1: execution process of Java program

Including: compile - > explain and execute two processes, among which:
① compilation: refers to compiling The source code of java is compiled by "[javac.exe compiler]" class bytecode file;
② explanation: it refers to loading by JVM class bytecode file and submit it to "[java.exe interpreter]" to explain the execution process.
PS: during specific execution, the code statements in the - main method body are executed line by line from the main method of the main class.

1.4.2 Part-2: JVM,Javac.exe,Java. Function and relationship of exe

1.4.2.1 JVM(Java Virtual Machine)

Java virtual machine is an application program running on the operating system. It is the implementation basis of Java platform independent characteristics. It has a complete architecture, including processor, Stack, register, etc., and a corresponding instruction system (bytecode is an instruction format).

	In a word,JVM is an application which can  read and execute byte-codes. 
Technically, JVM has its own memory model(Memory model) separates from OS.

1.4.2.2 javac.exe (java compiler)

Java compiler can convert java source file (. Java file) into Java bytecode file (. class file, a binary bytecode file that can be read and run by JVM)

1.4.2.3 java.exe (Java interpreter)

Java interpreter is a part of JVM, which can be understood as: Java interpreter is used to interpret bytecode files compiled by java compiler

*1.4. 2.4 relationship among the three

    javac.exe will be stored in *. In the operating system memory java source code files compile *. That the JVM can understand class binary bytecode file; Then, the JVM loads it into JVM memory (heap, stack, method area, constant pool, etc.) for java Exe is interpreted as the corresponding machine code line by line and executed line by line.

1.4.3 Part-3: brief description of object creation process

First of all, make it clear that [the premise of creating an object of class A is that the parent class of class A has been instantiated (in class A, the constructor of the parent class can be called through the super() statement)]
[1] compile Java source code files into bytecode files by Java compiler [javac.exe];
[2] the JVM transfers the bytecode file into the JVM memory through the class loader [ClassLoader];
[3] the Java interpreter interprets the bytecode file line by line and executes it.
The above process roughly explains the creation process of an object, that is:

Java The program encountered an error during execution new Keywords, you will check them first new After keyword[First used]of className Class pointed to A Whether it has a parent class,
- If so, it will be JVM take A The parent class of loads the parent class to through the class loader JVM In memory and through"super();"Priority creation A An instance of the parent class of, which is stored in JVM Memory heap area;
- If not, it will be created directly java.lang.Object An instance of the root parent class.
- Then load the class A Create an instance of a subclass based on the corresponding bytecode file[After the instance is created, the default initialization of member properties has been completed],And store it in JVM Memory heap area, through class A The constructor of carries out attribute assignment for subclass instances;
- Finally A Class object JVM The memory address of the heap area is returned to the reference variable in the stack for saving;
- The programmer can call through the reference object in the stack A Member methods and indirect access of class objects A Class object.

1.4.4 Part-4: object memory analysis

As shown below,

    

    

    

Class 2 and object examples

2.1 definition of person class

package com.msb.oop;

import java.io.Serializable;

/**
 * @Auther: xiwd
 * @Date: 2021/12/11 - 12 - 11 - 15:57
 * @Description: com.msb.oop
 * @version: 1.0
 */
public class Person implements Serializable {
    private static final long serialVersionUID = 2642102877515064840L;

    //properties-[static features noun]
    String name;
    int age;


    //methods-[dynamic features verb]
    //setter
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        if (age<0)
            return;
        this.age = age;
    }

    //getter
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

    /**
     * constructor without params-[[parameterless constructor]
     */
    public Person(){
        this(null,0);
    }

    /**
     * constructors with params-[[constructor with parameters]
     * @param name the object's name
     * @param age the object's age
     */
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    /**
     * get the specific object's information
     * @return String type-the content of information
     */
    public String introduce(){
        return "[name:"+this.name+",age:"+this.age+"]";
    }

    /**
     * eat food with a specific name
     * @param food food's name
     */
    public void eat(Food food){
        System.out.println(this.name+" eat "+food.getName());
    }
}

2.2 definition of food class

package com.msb.oop;

/**
 * @Auther: xiwd
 * @Date: 2021/12/11 - 12 - 11 - 16:12
 * @Description: com.msb.oop
 * @version: 1.0
 */
public class Food {
    //properties
    private String name;
    public byte aByte;
    public int anInt;
    public short aShort;
    public long aLong;
    public float aFloat;
    public double aDouble;
    public char aChar;
    public boolean aBoolean;
    public String string;

    //setter
    public void setName(String name) {
        this.name = name;
    }

    //getter
    public String getName() {
        return name;
    }

    //methods
    public Food(){
        this(null);
    }
    public Food(String name){
        super();
        this.name=name;
    }
}

2.3 main method test

package com.msb.oop.test;


import com.msb.oop.Food;
import com.msb.oop.Person;

/**
 * @Auther: xiwd
 * @Date: 2021/12/11 - 12 - 11 - 16:16
 * @Description: com.msb.oop
 * @version: 1.0
 */
public class Main_OOP {
    public static void main(String[] args) {
        Person person=new Person("ZhangSan",12);
        Food food=new Food("Apple");
        System.out.println(person.introduce());
        person.setAge(0);
        System.out.println(person.introduce());
        person.eat(food);

        System.out.println(food.aByte);//0
        System.out.println(food.anInt);//0
        System.out.println(food.aShort);//0
        System.out.println(food.aLong);//0
        System.out.println(food.aFloat);//0.0
        System.out.println(food.aDouble);//0.0
        System.out.println(food.aChar=='\u0000');//true
        System.out.println(food.aBoolean);//false
        System.out.println(food.string);//null
    }
}

Topics: Java Back-end