Common misunderstandings about java value passing and reference passing

Let's first look at a piece of code


public class Text16 {

    public static void main(String[] args) {
    	Circle demo = new Circle(5);
    	System.out.println(demo.radius);
    	change(demo);
    	System.out.println(demo.radius); 	
        Circle a = new Circle(1);
        Circle b = new Circle(2);
        method(a,b);
        System.out.println("一、a半径:"+a.radius+",b半径:"+b.radius);
    }
    public static void method(Circle x,Circle y){
        Circle tmp = x;
        x = y;
        y = tmp;
    }
    
    public static void change(Circle x)
    {
    	x.radius=0;
    }
    
}
class Circle{
    double radius;
    Circle(double x){
        radius = x;
    }
}


result:




Seeing this is a bit daunting

Because we often say that except for the eight basic types of objects that are passed by value, other objects are passed by reference.


And in the example we when we call

change(Circle x)
The method does change the value of the original object, isn't this just passing by reference?

But according to this reasoning when we call

 public static void method(Circle x,Circle y)
When the value of the two objects should be exchanged, but there is no, why is this?


The root cause of this problem is the misunderstanding of parameter passing in java:

The 8 basic types of objects are passed by value, and other objects are passed by reference


Actually not:
no matter basic type objects or other objects in java are all passed by value

Here the "value" can be simply understood as a "primitive types or objects in memory to address" (not accurate).

Calling

change(Circle x)

But it has not been implemented

x.radius=0;

Before, the memory was like this


Calling
x.radius=0;

After that, the memory is like this



We can analyze it like this. After calling the demo, the address is assigned to x, and the two point to the same memory space, and then the value of the radius changed by x, the value of the demo will naturally change.

In the same way, we analyze the call

 public static void method(Circle x,Circle y){
   
   

But before executing the code

The memory address is like this



a passes the address to x b passes the address to y a and x point to the same object b and y point to the same object

Then execute

 Circle tmp = x;
        x = y;
        y = tmp;

Memory map after


The addresses of x and y are exchanged, but the addresses of a and b are not affected, so the objects pointed to by a and b will naturally not change


If you understand, let's look at another example


Class User{
    private String name;
    .
    .
    .
}

public static void foo(User user){
    user.setName("Max");// AAA
    user = new User("Fifi");// BBB
    user.setName("Rowlf");// CCC
}

public static void main(String[] args){
     User basicUser = new User("Rover");
     foo(basicUser );
}

Excuse me, what is the name of the object basicUser after the three lines of code of'AAA','BBB', and'CCC' are executed?

'AAA' line: name is'Max'; 
'BBB' line: name is'Max'; 
'CCC' line: name is'Max';

Analyze by yourself


If you don’t understand the memory space heap and stack, I recommend a video explanation.

http://study.163.com/course/courseMain.htm?courseId=343001   

30 lessons and 31 lessons




Guess you like

Origin blog.csdn.net/a447332241/article/details/78939962