Java Basic Learning--17, object reference

Source: Internet
Author: User

We've been using the concept of "object" before, but we haven't explored how objects are stored in memory. This discussion will lead to the important concept of object reference.

Object Reference

We follow the previously defined human class and have a test class:

PublicClasstest{PublicStaticvoidMain (string[] args) {Human Aperson =New Human (160); }}Classhuman{/*** 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;}

The outside can invoke the class to create the object, such as the above in the test class:

New Human (160);

Created an object Aperson for the human class.

The above is a very simple statement, but we have a lot of detail to go into:

    1. First look at the right side of the equals sign. New is to open up space for objects in memory. Specifically, new is a space for objects on the heap of memory (heap). In this space, the data and methods of the object are saved.
    2. Look at the left side of the equals sign again. Aperson refers to a human object, called an object reference (Reference). In fact, Aperson is not the object itself, but rather a pointer to an object. The Aperson exists in the stack of memory (stack).
    3. When we assign a value with an equal sign, it is given to the object reference by the address of the right-hand new object created in the heap.

The memory here refers to the virtual Java process memory space of the JVM (Java Virtual machine). The heap and stack concepts of memory refer to Linux from program to process.

Object reference

The stack reads faster than the heap, but the data stored on the stack is limited by the valid range. In C, when a function call is finished, the corresponding stack frame is deleted, and the parameters and automatic variables stored on the stack frame disappear. Java stacks are also subject to the same restrictions, and when a method call is completed, the data stored on the stack is emptied. In Java, all (normal) objects are stored on the heap. Therefore,the full meaning of the new keyword is to create an object on the heap.

An object of the base type (primitive type), such as int, double, is saved on the stack. New is not required when we declare a basic type. Once declared, Java will store the underlying type of data directly on the stack. Therefore, the variable name of the base type represents the data itself, not the reference.

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

References and objects are separated; references point to Objects

Although references and objects are detached, all our access to the object must be referenced by the "gate", such as the method of accessing the object in a way that refers to the. method (). In Java, we cannot skip references to direct contact with objects. For example, if the data member of object A is a normal object b,a the data member holds a reference to object B (if it is a primitive type variable, then A's data member holds the basic type variable itself).

In Java, the reference acts as a pointer, but we cannot directly modify the value of the pointer, such as adding a pointer value of 1 to the C language. We can only perform operations on objects by reference. Such a design avoids the errors that many pointers can cause.

Assigned value of the reference

When we assign a reference to another reference, we are actually copying the address of the object. Two references will point to the same object. such as Dummyperson=aperson, will result in:

An object can have multiple references (a person can put more than one kite). When a program modifies an object through a reference, the modification is also visible through other references. We can test the actual effect using the test class:

Class test{    voidnew Human; Human Dummyperson = Aperson; System.out.println (Dummyperson.getheight ()); Aperson.growheight (a); System.out.println (Dummyperson.getheight ()); }}

Our changes to the Aperson will affect Dummyperson. These two references actually point to the same object.

Therefore, assigning a reference to another reference does not copy the object itself. We have to look for other mechanisms to replicate objects.

Garbage Collection

As the method call ends, the reference and basic type variables are emptied. Because the object survives the heap, the memory occupied by the object is not emptied with the end of the method call. Process space may soon be filled with objects that are constantly being created. Java has a garbage collection (garbage collection) mechanism for emptying objects that are no longer in use to reclaim memory space.

The basic principle of garbage collection is that when there is a reference to an object, the object is not recycled; When no reference is directed to an object, the object is emptied. The space it occupies is recycled.

Assumes the state of memory in the JVM at some point. The Human object has three references: the Aperson and Dummyperson from the stack, and the data member President of the other object. And club object has no references. If this time the garbage collection starts, then the club object is emptied, and the Human object Reference (President) from the club object is also deleted.

Garbage collection is an important mechanism in Java, which directly affects the efficiency of Java operation. I will delve into its details later.

parameter Passing

When we separate the concepts of references and objects, the parameter passing mechanism of the Java method is actually very clear: the Java parameter is passed as a value pass. That is, when we pass a parameter, the method obtains a copy of the parameter.

In fact, we pass arguments, one is a variable of the base type, and the other is a reference to the object.

The value of the underlying type variable is passed, meaning that the variable itself is copied and passed to the Java method. The Java method's modification of a variable does not affect the original variable.

The referenced value is passed, meaning that the object's address is copied and passed to the Java method. Access to the Java method based on that reference will affect the object.

Here's another case worth mentioning: we use new inside the method to create the object and return the object's reference. If the return is received by a reference, the object still exists and will not be garbage collected because the object's reference is not 0.

Java Basic Learning--17, object reference

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: 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.