1. Parameter transfer method
To solve the title problem, first introduce the parameter passing method. Currently, there are three main parameters transfer methods for various programming languages:
1. Pass by value
2. Pass by reference
3. Pass by pointer
The value-passing representation method (function) receives a copy of the variable provided by the caller without changing the value of the parameter; the value of the variable provided by the caller is passed by the reference; the value of the value (function) is received by the pointer by the pointer is received by the pointer without changing the value and address of the pointer, but the address pointed to by the pointer can be changed.
2. Java parameter transfer method
Unfortunately, there is only one parameter passing method provided by Java, which is passed by value. That is, the method obtains a copy of all parameter values, and the method cannot modify the content of the parameter variable passed to it.
Java method parameter types can be divided into two categories:
1. Basic data types
2. Object reference
Friends who have experience in Java development know that for basic data types, Java methods cannot change the content of variables. So can't the content be modified by reference to the object of the custom class? This can be explained by a simple example. The code is as follows:
pubpc class MyClass{ private Object num; pubpc MyClass(Object num){ this.num=num; } pubpc Object getNum() { return num; } pubpc void setNum(Object num) { this.num = num; }} pubpc class Main { pubpc static void change(MyClass myClass){ myClass.setNum(100); } pubpc static void main(String[] args){ MyClass a=new MyClass(10); System.out.println("The value before calling the change method is: "+a.getNum()); change(a); System.out.println("The value after calling the change method is: "+a.getNum()); } }The above code execution output results are as follows:
From the results, it can be seen that the change method can modify the state of the object. In other words, Java methods can change the parameter state of an object. So does this mean that Java methods use reference passes for parameters of custom data types (custom classes)? To confirm the results, you can write a simple example, and all custom classes are still MyClass above. The code is as follows:
pubpc static void swap(MyClass a,MyClass b){ MyClass tmp=a; a=b; b=tmp; } pubpc static void main(String[] args){ MyClass a=new MyClass(10); MyClass b=new MyClass(100); System.out.println("The value of a before swap is: "+a.getNum()); System.out.println("The value of b before swap is: "+b.getNum()); swap(a,b); System.out.println("The value of a after swap is: "+a.getNum()); System.out.println("The value of b after the exchange is: "+b.getNum()); }}The execution results are as follows:
From the above results, we can find that Java methods still use value passing for parameter passing of custom classes, not reference passing. Then why can Java methods modify object state?
You can consider the specific execution process of calling the change method to find the answer.
The specific execution process is:
myClass is initialized into a copy with the actual parameter a, which is a reference to the object.
The setNum method is applied to the reference of this object. The num of the MyClass object referenced by myClass and a is changed to 100.
After the method is finished, the parameter variable myClass is no longer used. And a continues to refer to the MyClass object whose num becomes 100. As shown in the figure below.
Therefore, the reason why Java methods can change the state of the object parameter is that the method obtains a copy of the object reference, and the object reference and other copies in the method refer to the same object at the same time.
Now, let’s summarize the Java method parameters:
Methods cannot modify parameters of a basic data type;
Methods can change the state of object parameters;
Methods cannot make object parameters refer to a new object (the reason is to refer to the specific execution process when calling the change method).
3. Exchange the numerical values of variables
Since you already know the reason, it is not difficult to know the reason. Post my personal code directly:
pubpc static void swap(MyClass a,MyClass b){ Object tmp=a.getNum(); a.setNum(b.getNum()); b.setNum(tmp);}The execution results are as follows:
The exchange is valid.
References: Java Core Technology Volume I.
The above article on Java exchanges the numerical implementation method of two variables is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.