In Java, there are two methods for passing parameters to functions: value transfer and reference transfer. Generally, the basic type is value transfer, and the reference type is reference transfer. But what happens when the value is passed?
1. Basic Type
The eight basic types (byte, short, Int, long, float, double, Char, Boolean) are passed values.
1 public class ValueTest { 2 3 public static void main(String[] args) { 4 int a = 10; 5 changeVal(a); 6 System.out.println(a); 7 } 8 9 public static void changeVal(int a){10 a += 10;11 }12 13 }
The demo above outputs 10 instead of 20. Why?
①. When the program runs, the main method first enters the stack, and then allocates memory to variable.
②. When running to changeval (a);, the changeval () method is added to the stack. When a method is added to the stack, memory is allocated to the local variable and the parameter variable (.
That is, in the changeval method stack, there is also a variable named a with a value of 10.
③ Because A + = 10; is run in the changeval method stack, it only changes the value in the changeval method stack and does not change the value in the main method stack. For example
2. Reference Type
The reference type is reference transfer, or transfer address.
1 public class ValueTest { 2 3 public static void main(String[] args) { 4 StringBuilder builder = new StringBuilder("hello"); 5 changeVal(builder); 6 System.out.println(builder); 7 } 8 9 public static void changeVal(StringBuilder builder){10 builder.append(" world");11 }12 13 }
The output is Hello world. The Builder value is changed.
Different from the basic type, stringbuilder is the reference type. Therefore, the memory of New stringbuilder ("hello") is allocated in the heap area. The variable (builder) in the stack area only saves one address (assuming 0x345), for example:
Therefore, the builder variables of the main and changeval method stacks point to the same memory. Therefore, when the builder value is changed in the changval method, the builder in main also changes.
3. "special" String
1 public class ValueTest { 2 3 public static void main(String[] args) { 4 String str = "hello"; 5 changeVal(str); 6 System.out.println(str); 7 } 8 9 public static void changeVal(String str){10 str+=" world";11 }12 13 }
The demo output above is: Hello, not Hello world. String is passed by reference, so the STR value should be changed?
This is related to the "feature" of string,String will be stored in the String constant pool, and string cannot be changed (final ).
①. Set a breakpoint in row 10th. When this line is executed, the analysis above shows that main () and changeval () str in the method all points to the "hello" string in the constant pool.
②. When STR + = "world" is executed, it is impossible to modify the string directly on "hello" because it is unchangeable. A "Hello World" string is added to the constant pool, and the address is passed to str in the changeval () method stack.
③. Therefore, str in the changeval () method points to the new "Hello World" string, while STR in the main method points to the original "hello ". Output "hello ".