Public class teststring {
Public static void Link (string ){
A + = "world ";
}
Public static void main (string ARGs []) {
String A = "hello ";
Link ();
System. Out. println ();
}
}
Output result: Hello
Resolution: The string type is passed as a parameter in Java.
JavaProgramIt is always executed from the main method. The main method defines the local variable A. When the program executes the Link () method, the system enters the Link () method and () the a variable in the method is used as a parameter to pass in the Link () method. The only thing that passes in the Link () method is a copy of a, not a itself, and enters the Link () two variables (two A) are generated in the system ). The system allocates two stack zones for main () and link () respectively to save the local variable A of the main () and link () methods. The program changes variable A to helloworld in the Link () method, but the value of variable A in the main () method has not changed.
The essence of value transfer: when the system starts to execute the method, the system performs initialization for the form parameter, that is, to assign the value of the real variable to the form parameter variable of the method, the operation in the method is not the actual real variable.
Public class demo01 {
Public static void Link (stringbuffer ){
A. append ("world ");
}
Public static void main (string ARGs []) {
Stringbuffer A = new stringbuffer ("hello ");
Link ();
System. Out. println ();
}
}
Running result: helloworld
Resolution: The stringbuffer type is passed as a parameter in Java and passed as a reference.
The program runs from the main method. The main method creates a stringbuffer object and defines a variable referenced by A to point to the stringbuffer object, which is different from the basic type. When an object is created, there are two items in the system memory: the object itself is saved in the heap memory, and the reference variables that reference the object are saved in the stack memory. Next, the program operates the stringbuffer object through reference. Next, the main method starts to call the Link () method. The main method is not over, and the system will open up two stack zones, main and link, local variables used to store the main and link methods. It is worth noting that a in the main method is a reference, which saves the address value of the stringbuffer object. When the value of A is assigned to the parameter of the link method, that is, let the parameter of the link method also save this address value, that is, it will also reference the stringbuffer object in the heap memory. This parameter transfer method is a value transfer method without compromise. The system copies the copy of A to the link () method, but the key is that A is only a reference variable, so the system copies the variable, however, the stringbuffer object is not copied. When the program operates the parameter in the Link () method, because a is only a reference variable, the actual operation is the stringbuffer object in the heap memory. At this time, whether it is the variable in the main () method or the parameter in the Link () method, it is actually the stringbuffer object referenced by it, they operate on the same object. Therefore, when parameter A in the Link () method changes, the of the stringbuffer object referenced by variable A in the man () method also changes.
Public class demo01 {
Public static void Link (stringbuilder ){
A. append ("world ");
}
Public static void main (string ARGs []) {
Stringbuilder A = new stringbuilder ("hello ");
Link ();
System. Out. println ();
}
}
Running result: helloworld
Resolution: stringbuilder is the same as stringbuffer. It is passed as a parameter for reference.