Java Interview Difficulties: Value Passing Mechanism

I. Introduction

Recently, I reviewed some basic knowledge of java, watched the javase video of Shang Silicon Valley, and learned a lot of new things, which are organized here to share with you as a technical blog, which is also convenient for you to learn and use.

Two, for basic data types

Next, we discuss the value transfer mechanism of method parameters .
First, let's first come to a piece of code:

public class ValueTransferTest1 {
    
    
    public static void main(String[] args) {
    
    
        int m = 10;
        int n = 20;

        //我们交换两个变量的值
        int temp = m;
        m = n;
        n = temp;

        //输出一下,看一下结果
        System.out.println("m = " + m + ", n = " + n);
    }
}
====================================================================================
输出结果为:
m = 20, n = 10

The output is as we are like, m = 20, n = 10

Next, we encapsulate the sorting operation in the above code into the swap method to call , the code is as follows:

public class ValueTransferTest1 {
    
    
    public static void main(String[] args) {
    
    
        int m = 10;
        int n = 20;

        //创建类对象,为了调用swap方法进行变量交换
        ValueTransferTest1 v = new ValueTransferTest1();
        v.swap(m,n);

        //输出一下,看一下结果
        System.out.println("m = " + m + ", n = " + n);
    }

    //交换两个变量值的方法
    public void swap(int m,int n){
    
    
        int temp = m;
        m = n;
        n = temp;
    }
}
====================================================================================
这个时候我们会发现,结果不再是m = 20, n = 10;

结果是:
m = 10, n = 20;

Why is the output result not swapped when the swap method of swapping variables is clearly called?
If we want to figure out the reason, we need to draw a memory analysis diagram :

Insert picture description hereAnalysis description:

首先,我们在栈内存中声明两个变量m,n赋值分别为10,20,这是在main主方法中。
我们接着调用了swap方法将m,n两个变量作为实参传递给swap方法中。
两个变量值进行交换。交换的变量属于swap方法,调用完swap方法,该方法就销毁出栈了。
所以输出的最终结果m,n其实是main方法中的m,n,最终结果就是m = 10, n = 20

That being the case, how can we solve such problems? The reference data type is about to be mentioned .

Three, for reference data types

Similarly, if you want to exchange variable values, define a swap method, but this time, the parameter is passed a reference type variable , not much to say, first look at the code as follows:

public class ValueTransferTest1 {
    
    
    public static void main(String[] args) {
    
    
        //创建Data对象
        Data data = new Data();
        //给属性m,n赋值
        data.m = 10;
        data.n = 20;
        //创建对象,调用swap方法,进行变量交换
        ValueTransferTest1 v= new ValueTransferTest1();
        v.swap(data);
        //输出结果
        System.out.println("m = " + data.m + ", n = " + data.n);
    }

    public void swap(Data data){
    
    
        int temp = data.m;
        data.m = data.n;
        data.n = temp;
    }
}

class Data{
    
    
    int m;
    int n;
}
====================================================================================
输出结果为:
m = 20, n = 10;

Why does the formal parameter pass a reference data type and the result is exchanged successfully?
We continue to look at the memory analysis diagram :

Insert picture description hereAnalysis description:

其实调用swap方法的时候,传的实参就是一个地址值。
毕竟引用类型变量存储的值不是null就是地址值。所有两个data变量指向同一个内存中的对象。
执行完swap方法后,方法销毁,导致tamp变量和data参数出栈销毁。
然而main方法中的data依旧指向内存中的0x7788,所以结果:m = 20, n = 10

4. Summary of value transfer mechanism (emphasis)

  • If the parameter is a basic data type , then the actual parameter assigned to the formal parameter is the actual data value stored in the actual parameter
  • If the parameter is a reference data type , then the actual parameter assigned to the formal parameter is the address value of the actual parameter storage data

If you can’t understand it, you can think of it this way:
In fact, the actual parameter is passed the basic data type , which can be understood as the copied value, no matter how the exchange is done, the original data is not exchanged, so the result does not change.
However, what the actual parameter passes is a reference data type , which can be understood as passing an address value , which points to the same address, so the same object is exchanged, and the result changes.

Guess you like

Origin blog.csdn.net/weixin_46594796/article/details/109532857