Introduction to Java (Java classes and objects - object references)

A reference to an object

    In the Java language, although everything can be regarded as an object, the real operation identifier is essentially a reference, so how is the reference reflected in Java?

    Syntax: class name object reference name

    eg : Book book;

    Usually a reference does not necessarily need to be associated with an object.

    To refer to the syntax associated with an object: 

Book book = new Book();

    Book : class name

    book : object

    new : create object operator

    A reference only stores the memory address of an object, not an object. Strictly speaking, a reference and an object are different, but this difference can be ignored. For example, it can be simply said that book is an object of the Book class, but in fact it should be book contains a reference to the Book object.

The comparison of objects

    In the Java language, there are two methods for comparing objects, namely the "==" operator and the equals() method. In fact, these two methods are fundamentally different.

    eg : Create a class that explains the difference between the "==" operator and the equals() method

public class Compare {
    public static void main(String[] args){
        String c1 = new String("abc"); //Create a String object reference c1
	String c2 = new String("abc"); //Create String object reference c2
	String c3=c1; //Assign c1 object reference to c3
	//Compare c2 with c3 using "==" operator
	System.out.println("The operation result of c2==c3 is: "+(c2 == c3));
        //Compare c1 with c3 using "==" operator
        System.out.println("The operation result of c1 == c3 is: "+(c1 == c3))
	//Use the equals() method to compare c2 and c3
	System.out.println("The result of the operation of c2.equals(c3) is: "+(c2.equals(c3)));
    }
}

    The running result is:

The operation result of c2 == c3 is: false
The operation result of c1 == c3 is: true
The operation result of c2.equals(c3) is: true

    It can be seen from the results that the contents compared between the "==" operator and the equals() method are different. The equals() method is a method in the String class, which is used to compare whether the contents referred to by two object references are equal; The "==" operator compares the addresses of two object references for equality. Since c1 and c2 are two different object references with different locations in memory, and the String c3 = c1 ; statement assigns the reference of c1 to c3, the two object references of c1 and c3 are equal, and also That is, a statement like printing c1 == c3 will return a true value.

3. Destruction of objects

    Each object has a unique life cycle. When the life cycle of the object ends, the memory address allocated to the object will be reclaimed. In other languages, Chinese and Western medicine manually recycle discarded objects, but Java has a complete garbage collection mechanism. Users do not have to worry about discarded objects occupying memory, and the garbage collector will recycle useless resources that occupy memory.

     There are two main situations in which objects are considered garbage by the Java Virtual Machine:

    (1) The object reference exceeds its scope, the object will be regarded as garbage;

    Such as:

{
Example e = new Example();
}
// refer to the object e outside the braces

    (2) Assign the object to null.

    Such as:

{
    Example e = new Example();
    e = null; //When the object is set to null, it will die
}

    Although the garbage collection mechanism is very perfect, the garbage collector can only recycle the objects created by the new operator. If some objects do not obtain a memory area in memory through the new operator, such objects may not be garbage collected. identified, so a finalize() method is provided in Java. This method is a method of the Object class, which is declared protected. Users can define this method in their own classes. If the user defines the finalize() method in the class, this method will be called first during garbage collection, and the memory occupied by the object can be truly reclaimed when the next garbage collection action occurs.

    Garbage collection or the finalize() method is not guaranteed to occur, such as when the Java virtual machine is running out of memory, it will not perform garbage collection.

    Since garbage collection is not under human control and the specific execution period is uncertain, the finalize() method cannot be executed. For this reason, Java provides the System.gc() method to force the garbage collector to start and tell the garbage collector to clean up.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631739&siteId=291194637