Java basics 11 Objects Reference Linux programs to processes

Source: Internet
Author: User
ArticleDirectory
    • Value assignment

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

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

 

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

 
Human aperson =NewHuman (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.NewIs to open up space for objects in the memory. Specifically, new is in the memoryHeap)Open up space for objects. 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 calledObject Reference). In fact, aperson is not an object, but a pointer to an object. Aperson exists in the memoryStack).
    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.

 

Object Reference

 

 

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,NewThe complete meaning of the keyword is,Create an object on the stack.

 

Primitive type)Objects, 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 (ProgramSee the kite (reference), but below the kite corresponds to a person (object ):

 

Separation of references and objects; reference pointing to objects

 

Although the reference and object are separated, all the accesses to the objectMust be referencedThis "Gate", for exampleReference. Method ()To access the object. 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 exampleDummyperson = 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 ClassTest {Public Static VoidMain (string [] ARGs) {HUMAN aperson=NewHuman (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,ReferenceAndBasic TypeThe variable is 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. Built in JavaGarbage Collection)Mechanism, used to clear objects that are no longer in use to reclaim memory space.

The basic principle of garbage collection is that when a reference points to an object, the object will not be recycled;When no reference points to an object, the object is 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 parameter passing of Java isValue Transfer. 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. Modify variables using the Java methodDoes not affect the original variable.

The passed referenced value means that the object address is copied and passed to the Java method. The Java methodWill 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

 

Welcome to continue reading the "Java quick tutorial" series of articles

 

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.