Java objects and references are confusing but basic knowledge that must be mastered. This article describes the concepts of Java objects and references and the transmission of parameters closely related to them.
First look at the following program:
Stringbuffer S;
S = new stringbuffer ("Hello world! ");
The first statement only allocates space for reference, and the second statement allocates space for reference by calling the stringbuffer constringbuffer (string Str) constringbuffer) generates an instance (or an object) for the class ). After these two operations are completed, the object content can be accessed through S-in Java, the object is manipulated through reference.
The relationship between Java objects and references can be said to be interrelated, but independent of each other. Mutual independence is mainly manifested in: reference can be changed, it can point to another object, such as the above S, you can give it another object, such:
S = new stringbuffer ("Java ");
In this way, S is separated from the first object it points.
In terms of storage space, objects and references are also independent. They are stored in different places, objects are generally stored in the heap, and references are stored in a faster stack.
References can point to different objects, and objects can be manipulated by multiple references, for example:
Stringbuffer S1 = s;
This statement points S1 and S to the same object. Since two references direct to the same object, no matter which reference is used to manipulate the object, the content of the object changes, and there is only one copy. The content obtained through S1 and S is naturally the same, the following procedure:
Stringbuffer S;
S = new stringbuffer ("Java ");
Stringbuffer S1 = s;
S1.append ("world ");
System. Out. println ("S1 =" + s1.tostring (); // The output is S1 = Java World.
System. Out. println ("s =" + S. tostring (); // The output is: S = Java World.
The above program shows that the content printed by S1 and S is the same, and this result looks confusing, but think about it carefully. S1 and S are just two references, and they are just joystick, they direct to the same object. They manipulate the same object and get the content of the same object. This is like the brakes and throttling of a car. They manipulate the speed of a car. If the speed of a car starts to 80, you step on the throttling and the car is accelerated. If the speed of a car is increased to 120, then you step on the brakes, and the speed starts to decrease from 120. If you drop to 60 and then step on the accelerator, the speed starts to rise from 60, instead of starting from 120 after the first step on the accelerator. That is to say, the speed is affected by both the throttling and the brakes, but they are accumulated rather than independent (unless the brakes and the throttling are not in a car ). Therefore, in the above program, no matter S1 or S is used to manipulate objects, their impact on objects also accumulates (more references are the same ).
Parameter passing can be understood only when the relationship between objects and references is understood.
Generally, questions about passing parameters in Java are taken into consideration in the interview questions. The standard answer is that Java only has one parameter passing method: passing by value, that is, passing everything in Java is passing values. If the input method is of the basic type, you will get a copy of the basic type. If a reference is passed, a copy of the reference is obtained.
In general, we can easily understand the transmission of basic types. For objects, we always feel like passing by reference. Let's look at the following program:
Public class objectref {
// Transfer Parameters of the basic type
Public static void testbasictype (INT m ){
System. Out. println ("m =" + M); // M = 50
M = 100;
System. Out. println ("m =" + M); // M = 100
}
// The parameter is an object and does not change the reference value
Public static void add (stringbuffer s ){
S. append ("_ Add ");
}
// The parameter is an object and the referenced value is changed.
Public static void changeref (stringbuffer s ){
S = new stringbuffer ("Java ");
}
Public static void main (string [] ARGs ){
Int I = 50;
Testbasictype (I );
System. Out. println (I); // I = 50
Stringbuffer Smain = new stringbuffer ("init ");
System. Out. println ("Smain =" + Smain. tostring (); // Smain = init
Add (Smain );
System. Out. println ("Smain =" + Smain. tostring (); // Smain = init_add
Changeref (Smain );
System. Out. println ("Smain =" + Smain. tostring (); // Smain = init_add
}
}
The above results show that the testbasictype method parameter is the basic type, although the m value is changed, but does not affect I.
The parameter of the add method is an object. When Smain is passed to parameter S, s obtains the copy of Smain, so s and Smain point to the same object. Therefore, the s operation actually affects the object pointed to by Smain. Therefore, after the add method is called, the content of the object pointed to by Smain changes.
In the changeref method, parameters are also objects. When Smain is passed to parameter S, s gets the copy of Smain, but unlike the add method, in the method body, the object s points to (that is, s points to another object) is changed. After a value is assigned to S, S has no association with Smain, smain and Smain point to different objects. Therefore, no matter what operations are performed on S, the object to which Smain points will not be affected. Therefore, the content of the object that Smain points to before and after the changeref method is called has not changed.
Many people may feel this way about the calling result of the add method: Isn't it passed by reference clearly? To solve this problem, use Bruce Eckel: It depends on how you think about references. In the end, you will understand that this argument is not that important. What really matters is that you need to understand that transferring a reference makes the modification of the (caller's) object unpredictable.
There is only one answer: in Java, parameters are passed by value. In fact, we need to understand what happens when the parameter is an object, just like the add method above ......