Java value passing comprehension, java value passing Comprehension
Java does not pass references only by value, but does not pass references only by value.
The following code explains:
1 public class Test { 2 public static void main(String[] args ){ 3 int var = 1 ; 4 f() ; 5 System.out.println(var) ; 6 } 7 public static void f(int newVar ){ 8 newVar = 2 ; 9 }10 }
Execution result:
1
Analysis:
When int var = 1 is executed, jvm opens a space in the stack to store value --- 1, and the var variable points to the memory space corresponding to value 1, that is, the var variable also has its own memory space, but its space stores the memory address corresponding to value 1.
When you execute Row 7 and pass the var value to method f, the jvm executes the operation to create a new variable newVar, copy the value stored in var (that is, the memory address of value 1) to newVar.
When the eighth row is executed, the jvm re-opens the value -- the bucket corresponding to 2 to store value 2, and modifies the address value stored in newVar.
Method return, execute to the fifth line, and output the value pointed to by var. Of course, the value is 1, because the address value in var is not changed at all.
The above example uses int, a basic data type, which is also applicable to referenced data types, because there is no reference transfer in java and only value transfer. For example, the custom class UserClass:
1 class MyClass { 2 int var1 ; 3 } 4 public class Test { 5 public static void main(String[] args ){ 6 MyClass myClass = new MyClass() ; 7 myClass.var1 = 1 ; 8 f(myClass) ; 9 System.out.println(myClass.var1) ; 10 }11 public static void f(MyClass newMyClass ){12 myClass.var1 = 100 ;13 }14 }
Execution result:
100
Analysis:
Row 6: Create a variable in the stack to point to the object created in the stack. The storage of object attributes in the heap can be understood as allocating a piece of memory space to the object in the heap for every creation of an object. This memory space records the class of the object, the rest is to store the property value of the object.
Row 7: Modify Object Property values:
The following is the key eighth line of value transfer. jvm will open up a space newMyClass in the stack, and then copy the content in myClass into newMyClass, in this way, the modification to newMyClass in method f is the modification to the content corresponding to MyClass, because newMyClass also points to the same memory space as myClass.
The modification to the newMyClass attribute in row 12th also affects the content of myClass, because they both refer to the same memory space.
Finally, the returned content of myClass is modified.
The following describes a special class ---- String, which is a special reference type in java. When passed using String, it is found that the modification in the method does not affect the value outside the method, for example, the following code:
1 public class Test { 2 public static void main(String[] args ){ 3 String var = "Hello" ; 4 f(var) ; 5 System.out.println(var) ; 6 } 7 public static void f(String newVar ){ 8 newVar = "World" ; 9 }10 }
Running result:
Hello
Before analyzing this code, you need to know how the underlying layer of the String is implemented. In fact, the String is implemented through the char array. Simply put, the String is an encapsulation of the char. You must also understand that the effect of direct "String content" is the same as that of using new String ("String content"), except that the jvm stores different strings. To analyze the code above, we also need to use the reference data value passing method for analysis.
The third line of jvm stores the created "Hello" string in the static storage area, and opens the address of var pointing to Hello in the stack (the value stored in var is the address of the Hello string ). When var is passed as a parameter, jvm creates a new variable newVar in the stack, and newVar points to the "Hello" memory zone, however, in row 8 newVar = "World", the "Hello" address stored in newVar is changed to the address of a newly created "World" String object. Note, the address stored in var is still "Hello", so the output is still "Hello" when the method returns.
I finally finished writing ~~~ I found that I wanted to write something like '(*> memory <*)'. There must be an error. I didn't make it clear in the memory allocation, because I didn't know much about it either, there is also the memory allocation of properties in the object. I really don't know about this. Which one knows, don't forget to tell me ~~