Java basics 11 Object Reference

Source: Internet
Author: User

Java basics 11 Object Reference
We have been using the concept of "object" before, but we have not explored the specific storage method of objects in memory. This discussion will introduce the important concept of "object reference. Object Reference

We use the previously defined Human class and have a Test class:

 

public class Test{    public static void main(String[] args)    {        Human aPerson = new Human(160);                     }}class Human{       /**     * constructor     */    public Human(int h)    {        this.height = h;    }    /**     * accessor     */    public int getHeight()    {       return this.height;    }    /**     * mutator     */    public void growHeight(int h)    {        this.height = this.height + h;    }    private int height;}

 

You can call a class to create an object externally, for example, in the Test class above:

Human aPerson = new Human(160);

Creates a Human Class Object aPerson.

The above is a very simple expression, but we have many details to go:

  1. First, check the right side of the equal sign. New is to open up space for objects in the memory. Specifically, new is to open up space for objects in the heap of the memory. This space stores the data and methods of objects.
  2. Let's look at the left side of the equal sign. APerson refers to a Human object, which is called an object reference ). In fact, aPerson is not an object, but a pointer to an object. APerson exists in the memory stack.
  3. When we use the equal sign to assign a value, the address of the object created in the new heap on the right is assigned to the object reference.

    The memory here refers to the memory space of Java processes virtualized by JVM (Java Virtual Machine. For the concept of memory heap and stack, refer to Linux from program to process.

     

     

    The read speed of the stack is faster than that of the stack, but the data stored on the stack is limited by the effective range. In C language, when the callback function is called, the corresponding stack frame will be deleted, and the parameters and automatic variables stored on the stack frame will disappear. Java stacks are also subject to the same restrictions. When a method call ends, the data stored on the stack will be cleared. In Java, all (common) objects are stored on the stack. Therefore, the full meaning of the new Keyword is to create an object on the stack.

    Objects of the basic type (primitive type), such as int and double, are stored on the stack. New is not required when we declare the basic type. Once declared, Java will directly store basic types of data on the stack. Therefore, the variable name of the basic type indicates the data itself, not the reference.

    The relationship between references and objects is like a kite and a person. When we look at the sky (written in the Program), we see a kite (reference), but the kite corresponds to a person (object ):


    Separation of references and objects; reference pointing to objects

     

    Although references and objects are separated, all access to objects must reference this "Gate", for example, accessing methods of objects by referencing. Method. In Java, we cannot skip references to directly access objects. For another example, if the data member of object a is a common object B, the data member of object a saves the reference pointing to object B (if it is a basic type variable, the data member of a saves the basic type variable itself ).

    In Java, the reference serves as a pointer, but we cannot directly modify the pointer value. For example, we add the pointer value to 1 as in C. We can only perform operations on objects by referencing them. This design avoids many pointer errors.

     

    Value assignment

    When we assign a reference value to another reference, we actually copy the object address. The two references point to the same object. For example, dummyPerson = aPerson; will cause:


     

    An object can have multiple references (one person can put multiple kites ). When the program modifies an object through a reference, the modification can also be seen through other references. We can use the following Test class to Test the actual effect:

     

    public class Test{    public static void main(String[] args)        {             Human aPerson = new Human(160);             Human dummyPerson = aPerson;             System.out.println(dummyPerson.getHeight());             aPerson.growHeight(20);             System.out.println(dummyPerson.getHeight());        }}

     

    Our changes to aPerson will affect dummyPerson. The two references actually point to the same object.

     

    Therefore, assigning a reference to another reference does not copy the object itself. We must seek other mechanisms to copy objects.

     

    Garbage Collection

    As the method call ends, the reference and basic type variables are cleared. Because the object exists in the heap, the memory occupied by the object will not be cleared as the method call ends. The process space may soon be filled with constantly created objects. Java has a garbage collection mechanism, which is used to clear objects that are no longer in use to recycle memory space.

    The basic principle of garbage collection is that when a reference points to an object, the object will not be recycled. If no reference points to an object, the object will be cleared. The space occupied by it is recycled.


     

    Assume the memory status in the JVM at a certain time point. The Human Object has three references: aPerson and dummyPerson from the stack, and the data member president of another Object. Club Object is not referenced. If garbage collection is started at this time, the Club Object will be cleared, and the reference (president) of the Human Object from the Club Object will also be deleted.

     

    Garbage collection is an important mechanism in Java, and it directly affects the running efficiency of Java. I will go into details later.

     

    Parameter transfer

    After we have separated the concepts of references and objects, the parameter passing mechanism of the Java method is actually very clear: the Java parameter passing is a value passing. That is to say, when we pass a parameter, the method will get a copy of the parameter.

    In fact, one of the parameters we pass is a basic type variable, and the other is an object reference.

    Passing the value of a basic type variable means that the variable itself is copied and passed to the Java method. The modification to the variable by the Java method does not affect the original variable.

    The passed referenced value means that the object address is copied and passed to the Java method. The access of the referenced Java method will affect the object.

     

    Here is another case worth mentioning: We use new to create an object in the method and return the reference of this object. If the returned result is received by a reference, because the object reference is not 0, the object still exists and will not be recycled.

     

    Summary

    New

    Reference, Object

    Condition for garbage collection

    Parameter: Value Transfer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.