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 assignmentWhen 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 CollectionAs 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 transferAfter 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.
SummaryNew
Reference, Object
Condition for garbage collection
Parameter: Value Transfer