1. Introduction: Assignment of variables

For basic data types

int m = 1;
int n = 2;
System.out.println("m = " + m);
System.out.println("n = " + n);
n = m;
System.out.println("n = "+ n); Output m =1
n = 2
n = 1
Copy the code

The assigned value is the variable value.

For reference types

class User{
    int a;
}

User u1 = new User();
u1.a = 1;
User u2 = u1;
System.out.println("A in U1 is" + u1.a + "," + "A in U2 is" + u2.a);
u2.a = 2;
System.out.println("A in U1 is" + u1.a + "," + "A in U2 is"+ u2.a); A in the output U1 is1A in U2 is1A in U1 is2A in U2 is2
Copy the code

The assigned value is the address value.

If you have some questions about the second one, let me draw a crude memory parsing to help you understand it.

The first5Lines of code,newA User object is stored in heap space, and the initial value of a is0.The local variable U1 is stored on the stack, and the value of U1 is the address value of the User object0x3336Line of code, u1 modifies the value of A, so that a becomes17Line of code, there's a u2 on the stack, and it assigns u2 the address of U1, so u2 is also zero0x3339Line of code, u2 modifies the value of A, that is, a becomes2
Copy the code

2. Value transfer mechanism

First, make it clear that this applies primarily to method calls, that is, to the arguments passed to the method by its formal participation.

Parameter passing mechanism in method: value passing

(1) If the argument is a primitive data type, the argument is passed the actual stored data value to the parameter

(2) If the argument is a reference type, the argument is passed the address value to the parameter

For example, to swap the values of AAA and BBB, there is the swapswapswap function

void swap(int a,int b){
    int t = a;
    a = b;
    b = t;
}
Copy the code

If this function is called at this point, does it swap?

int x = 1;
int y = 2;
swap(x,y);
Copy the code

The answer is no! Here is a simple memory map to parse

It can be seen that swapswapswap only swaps parameters in its own method, not XXX and YYy, because aaa and BBB are only copy values of arguments XXX and YYy. At the end of the method, they are recycled.

And if I ask again, does swapswapswap, the following function, swap?

void swap(User a, User b){
    User t = a;
    a = b;
    b = t;
}
Copy the code
User u1 = new User();
User u2 = new User();
swap(u1,u2);

class User{}Copy the code

Again, the answer is no!

As you can see, when I do this, the blue arrow is not affected at all.

So how do you do that? We can find some answers by going back to the introduction of assigning variables to reference types.

void swap(int[] array,int a,int b){
    int t = array[a];
    array[a] = array[b];
    array[b] = t;
}
Copy the code

Such an exchange function, can play the exchange function!

int[] h = {1.2};
swap(h,0.1);
System.out.println(h[0] + "," h[1]); The output2.1
Copy the code

That is, a method cannot modify the parameters of an underlying data type, but it can modify the state of the object to which a reference type refers.

That is, when we pass an argument of a reference type, the argument gets its address. We can modify the argument by modifying the parameter.

3. Interesting thought questions

(1)

public class Test{
    public static void main(String[] args){
        int a = 10;
        int b = 20;
        method(a,b);// After calling method, only a=100, b=200 are printed
        System.out.println("a="+a);
        System.out.println("b="+b);
    }
    // The code department
    public static void method(int a,int b){}}Copy the code

(2)

int[] arr = {1.2.3};
System.out.println(arr);
char[] c = {a,b,c}; System.out.println(c); Output The second line output: ________ the fourth line output: ________Copy the code

The answer

(1)

public static void method(int a,int b){
    System.out.println("a=" + a*10);
    System.out.println("b=" + b*10);
    System.exit(0);
}
Copy the code

(2)

Arrarrarr Address value


a b c abc

4. Reference materials

(1) Silicon Valley _Java Zero basic tutorial – A prerequisite for Java entry – a complete set of tutorials for beginners (song Hongkang)

(2) A detailed explanation of the difference between Java basic data type passing and reference passing


For more Java content, please follow my official account ACJavaBear, where I will post the first time. Let’s learn Java