Surpass Day5 - Java Object Oriented Creation and Use 1

Catalog

1. Members/Instances/Object Variables, Instances (Objects)

1.1 Basic Properties of Objects

1.2 Member variables, instance variables, static variables (class variables), local variables

2. Creation and use of objects

2.1 Definitions of objects and references

2.2 Object Creation

2.3 JVM Three Memories

2.4 Object Access

2.5 Instances

2.6 Couples

2.7 JVM

2.7.1 Memory Area

2.7.2 Garbage Collector [Automatic Garbage Collection Mechanism, GC Mechanism]

2.8 null pointer exception

1.1 Basic Properties of Objects

1) Class body = Attribute + Method, Attribute [Store data as variable];

2) Objects are also called instances, and instance variables are also called object variables (object-level variables);

3) Without creating an object, the memory space of this variable does not exist. Only when an object is created, the memory space of this variable will be created.

1.2 Member variables, instance variables, static variables (class variables), local variables

1) Member variables: scope is the entire class, equivalent to global variables in C, defined outside the method body and statement block, generally defined in the class body; Member variables include instance variables and static variables (class variables);

If member variables are not assigned manually, the system defaults are as follows:

Data Type Default
​
------------------------
​
byte,short,int,long                     0;
​
float,double                             0;
​
boolean                               false;
​
char                                  \U000;
​
Reference data type * null;

2) Instance variables: variables independent of methods, declared in a class without static modification, but outside methods, construction methods, and statement blocks, numeric variables default to 0, Boolean default to false, and reference types default to null;

Instance variables cannot be accessed through the Class Name; they must be referenced.

Instance variables are stored in java object memory in heap memory;

3) Static variables (class variables): variables independent of methods, decorated with static, default values are similar to instance variables, only one in a class is common to objects, stored in (method area memory) static storage, often declared as constant, and the call is generally a class name. Static variable name, or object name. Static variable name call;

public static String level = "SSS"; *// Member variables, static variables (class variables)*

4) Local variables: variables in a class's methods, access modifiers cannot be used for local variables, declare that in a method, construction method, or statement block, they are allocated on the stack, have no default values, and must be initialized;

2. Creation and use of objects

2.1 Definitions of objects and references

The memory space that the new operator opens up in the stack memory is called an object.

A reference is a variable, except that it holds the memory address of another java object.

1) Unlike pointers in C, pointers can point to any block of address in memory space

2) There is no pointer in java, and heap memory cannot be manipulated directly. Only instance variables of objects in heap memory can be accessed by reference, which ensures the security of Java language.)

2.2 Object Creation

1) Multiple objects can be instantiated through one class. The syntax for instantiation is: new class name ();

2) new is an operator in the java language that creates objects and opens up new memory space in the JVM heap memory.

int i = 10;

Int is a basic data type; i is a variable name; 10 is a literal value of type int;

Student s = new Student( );

Student is a reference data type; s is a local variable [stored in stack memory] representing a reference; new Student () is to create a student object;

2.3 JVM Three Memories

Method memory: Class byte code snippets are loaded into the memory space when the class is loaded;

Stack memory (local variable): The method code snippet allocates memory space to the method when it executes, stacking in the stack memory; *

Heap memory: new objects are stored in heap memory;

 

2.4 Object Access

Syntax format for accessing instance variables:

Read data: reference. Variable name;

Modify data: reference. Variable name = value;

2.5 Instances

//User class
public class User{
    int no;//Basic data type instance variable 
    String name;//Reference data type instance variable
    Address addr;//Reference data type instance variable
}
//Home Address Class
public class Address{
    String city;//The reference data type instance variable city is a reference: a variable that holds a memory address that points to heap memory;
    String street;
    String zipcode;
}
//Test Class 1
public class helloworld{
    public static void main(String[] args){
        User u = new User();
         System.out.println(u.no);
         System.out.println(u.name);
         System.out.println(u.addr);
         u.no = 110;
         u.name = "jack";
        u.addr = new Address();
    }
}

 

 

//Test Class 2
public class helloworld{
    public static void main(String[] args){
        User u = new User();
        Address a = new Address();
        u.addr = a;
        System.out.println(u.addr.city);
        a.city = "Tianjin";
        System.out.println(u.addr.city);
    }
}

 

 

2.6 Couples

//Husbands
public class Wife{
    String name;
    Husband h;//Wife citation included in husband object
}
//Wives
public class Husband{
    String name;
    Wife w;//There are also references from husbands among wives
}
public class code{
    public static void main(String[] args){
        //Create a husband object
        Husband huang = new Husband();
        huang.name = "Huang Xiaoming";
        //Create a wife object
        Wife baby = new Wife();
        baby.name = "baby";
        //Marriage
        huang.w = baby;
        baby.h = huang;
        //Get the name of Huang Xiaoming's wife
        System.out.println(huang.name + "The wife's name is" + huang.w);
    }
}

2.7 JVM

2.7.1 Memory Area

Among them, stack memory is the most frequently changed, method of memory removal is the first data, and garbage collector is mainly for heap memory.

2.7.2 Garbage Collector [Automatic Garbage Collection Mechanism, GC Mechanism]

When would you consider reclaiming memory for a java object?

1) When a java object in the stack memory is called garbage data, it will be recycled by the garbage collector;

2) When will java objects in heap memory become garbage? - When no more references point to it;

-This object cannot be accessed because it can only be accessed by reference;

2.8 null pointer exception

public class code{
    public static void main(String[] args){
        Customer c = new Customer();
        System.out.println(c.id);
        c=null;
        //The following program can be compiled through because the //run has a null pointer exception because of the syntax
        //A null reference accessing instance-related data must have a null pointer exception
        //java.lang.NullPointerException
        System.out.println(c.id);
    }
}

 

Keywords: Java Eclipse Back-end

Added by mdl on Tue, 04 Jan 2022 16:14:09 +0200