As a rare Java girl, all the blogs she wrote were just for her notes, leaving evidence that she had studied hard before - hahahaha (if there is something wrong, please point out, don't let me know quietly)
Preface
Last week, when I wrote a piece of code, there was a bug that made me blush. Let me show you the sample code. The original business code will not be pasted, anyway, it means the same thing.
//eg1 public class ArrayListDemo { public static void testObject(DoubleDemo doubleDemo){ DoubleDemo doubleDemo1 = new DoubleDemo(); doubleDemo1.setName("testObject1"); doubleDemo = doubleDemo1;① } public static void main(String[] args) { DoubleDemo doubleDemo = new DoubleDemo(); doubleDemo.setName("main"); testObject(doubleDemo);② System.out.println(doubleDemo.getName());//main } }
I thought I would output testObject1, but it didn't work out as I wanted. Why did it happen?
Value transfer and reference transfer
In Java, the method parameters of basic data type are all value passing, and the variables are modified inside the method without changing the values of variables outside the method.
//eg2 public static void testObject(int i){ i = 20; } public static void main(String[] args) { int i=10; System.out.println(i);//10 }
The method parameter of Java reference type is reference passing. Modifying the value of the object through reference inside the method will really change the value of the object in the heap.
//eg3 public static void testObject(DoubleDemo doubleDemo){ doubleDemo.setName("testObject"); } public static void main(String[] args) { DoubleDemo doubleDemo = new DoubleDemo(); doubleDemo.setName("main"); testObject(doubleDemo); System.out.println(doubleDemo.getName());//testObject }
Back to the top example, in the testObject method, the handling of doubleDemo is different. One is to change the value of the parameter object through the set method in the method, and return the value of the object in the main function; the other is to assign another reference to doubleDemo in the method, and return whether doubleDemo in the main function points to the original object. This is because the assignment within the local method only takes effect within the local method. To return to the calling function, doubleDemo still points to the original memory address. To change the property directly through the set method is to change the value of the object in the heap directly.
So in daily development, there are two ways to change the property value of an object by calling a method:
1. Use the set method to directly change the property value of the object inside the method.
2. Assign value through = in the method, but receive the object in the calling method, that is, the local method should have return value.
public class ArrayListDemo { //eg4 public static void testObject(DoubleDemo doubleDemo){ DoubleDemo doubleDemo1 = new DoubleDemo(); doubleDemo1.setName("testObject1"); doubleDemo = doubleDemo1; } public static void main(String[] args) { DoubleDemo doubleDemo = new DoubleDemo(); doubleDemo.setName("main"); //Assign the result of local method to the main function again doubleDemo = testObject(doubleDemo); System.out.println(doubleDemo.getName());//testObject1 } }
Let's debug the code in eg1, which is the example at the beginning of the article. When executing to position ①, you can see that doubleDemo turns blue (local variable), and the attribute of name turns testObject1.
Continue to ② and see that the name attribute of doubleDemo changes to main.