Java Basics Tutorial Object reference _java

Source: Internet
Author: User
Tags garbage collection

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

Object reference

We followed the human class defined previously and had a test class:

Copy Code code as follows:

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;
}


An external can invoke a class to create an object, such as the above in the test class:

Copy Code code as follows:

Human Aperson = new Human (160);

Created an object Aperson for the human class.

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

1. First look to the right of the equal sign. New is to open up space for objects in memory. Specifically, new creates space for objects on the heap of memory (heap). In this space, the data and methods of the object are saved.

2. Look to the left of the equal sign again. Aperson refers to a human object, called an object reference (reference). In fact, Aperson is not the object itself, but is similar to a pointer to an object. Aperson exist in memory stacks (stack).

3. When we assign a value with the equal sign, we give the object a reference to the address of the right new object created in the heap.

The memory here refers to the Java process memory space that is virtualized by the JVM (Java Virtual Machine). The memory heap and stack concepts refer to Linux from the program to the process.

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 ends, the corresponding stack frame (stack frame) is deleted, and the parameters stored on the stack frame and the automatic variable disappear. Java stacks are also subject to the same restrictions, and when a method call ends, the data stored on the stack by the method is emptied. In Java, all (normal) objects are stored on the heap. Therefore, the complete meaning of the new keyword is to create an object on the heap.

Objects of the base type (primitive type), such as int, double, are saved on the stack. When we declare a basic type, we do not need new. 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), see the Kite (reference), but the kite corresponds to the following, is the person (object):

Separation of references and objects; References to Objects

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

In Java, references play a role in pointers, but we can't directly modify the values of pointers, such as the C language, which adds a pointer value of 1. We can only perform operations on objects by reference. Such a design avoids many errors that can be caused by pointers.

Assigned value of a reference

When we assign a reference to another reference, we actually copy 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 (one can put multiple kites). When a program modifies an object through a reference, the modification is also visible through other references. We can test the actual effect with the following test class:

Copy Code code as follows:

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 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 must look for other mechanisms to replicate the object.

Garbage collection

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

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

The diagram above assumes the state of memory in the JVM at some point. Human object has three references: the Aperson and Dummyperson from the stack, and the data member President of another object. And the club object is not referenced. If garbage collection starts at this time, the club object is emptied, and the human object from the club object's Reference (President) is deleted.

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

Parameter passing

When we detach the concept of references and objects, the parameter-passing mechanism of the Java method is actually very clear: Java parameter passing is a value pass. That is, when we pass a parameter, the method obtains a copy of the parameter.

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

The value of the base 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 address of the object is replicated and passed to the Java method. The Java method's access to the reference will affect the object.

Here's another case worth mentioning: we use new to create an object inside the method and return the reference to that object. 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.

Summarize

New

References, objects

Conditions for being garbage collected

Parameters: Value Passing

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.