Should Java parameters be passed by value or by reference? This question is very basic, but many people are a little confused
First we have to understand several concepts about parameters
Formal parameters: The parameters used when defining a function are used to receive the parameters passed in by the function. For example, if we write a function, the parameters in the function are formal parameters.
public void test(String str) { //str为形式参数 System.out.println(str); }
Actual parameters: We call the function When, the parameters in parentheses after the function name are called actual parameters and must have definite values, as shown in the following example
public static void main(String[] args) { A a = new A(); a.test("小 明"); //"小 明"则为实际参数 }
It can be found that when a parameterized function is called, the actual parameters will be passed to Formal parameters.
There are generally two situations for parameters in this kind of passing processValue passing and reference passing.
Value passing: When calling the function, copy the actual parameters and pass them to the function. Modifying the parameters inside the function will not affect the actual parameters. Parameters, that is, creates a copy, which will not affect the native object
does not create a copy, but will affect the native object
basic data types and reference data types . Except for 8 basic data types, are all reference data types, namely byte, short ,int,long,char,boolean,float,double
public class TestBasic { public static void main(String[] args) { int num1 = 10; int num2 = 20; change(num1, num2); System.out.println("=============="); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); } public static void change(int param1, int param2) { System.out.println("param1 = " + param1); System.out.println("param2 = " + param2); param1 = 333; param2 = 444; System.out.println("after change...."); System.out.println("param1 = " + param1); System.out.println("param2 = " + param2); } }
param1 = 10We can find that reassigning variables in the change() method does not change the values of variables num1 and num2.param2 = 20
after change....
param1 = 333
param2 = 444
==============
num1 = 10
num2 = 20
What changes is only the copies of num1 and num2 in the change() method. . We need to know that basic data types have only one storage space in memory, which is allocated in the stack.
If the type of Java parameters is a basic data type, it ispassed by value.
Parameter type: reference data typepublic class TestQuote { public static void main(String[] args) { String str = "小明"; StringBuilder str2 = new StringBuilder("今天天气好"); change(str,str2); System.out.println("=============="); System.out.println("str = " + str); System.out.println("str2 = " + str2); } public static void change(String param1,StringBuilder param2) { System.out.println("param1 = " + param1); System.out.println("param2 = " + param2); param1= "小张"; param2.append(",我们去钓鱼"); System.out.println("after change...."); System.out.println("param1 = " + param1); System.out.println("param2 = " + param2); } }
param1 = Xiao MingWe found that the str variable has not changed, but the str2 variable has changed. Are you confused: if the type of Java parameter passing is a reference data type, it isparam2 = Today The weather is good
after change....
param1 = Xiao Zhang
param2 = The weather is good today, let’s go fishing
str = Xiao Ming
str2 = The weather is good today , let’s go fishing
value passing or passed by reference ?
In fact, everyone has been fooled by a bunch of terminology. The author drew 2 pictures to help everyone understand: before change():after change(): In Java, except for basic data types, others are reference types. Reference types have two storage spaces in memory. (One piece is in the stack and one piece is in the heap). If the parameter is a reference type, what is passed is a copy of the address value of the object referenced by the actual parameter on the stack. The copy created here is a copy of the address. Then someone said it, but its value has changed. Is this obviously "pass by reference"? We can understand it from another perspective. If we treat the stack address as a value, a copy of the stack address will be created (copy the stack frame). The stack address does not change in the end, but the value in the heap memory changes. It's like the stack address is a key. If we copy it, it can open the safe. What we care about is whether there is a change in the pattern of the key. As for the amount of money after opening the safe, we don't need to care. Although after calling the function, the str2 variable value (data in the heap) has changed, but the parameter is a reference type, and the actual parameter passed is the address value in the stack. This is what we care about, and the stack is copied. The address value in the final stack has not changed. Therefore, creating a copy conforms to the definition of
value transfer and will not affect the original object.
Someone may ask again, why does the str variable value not change? In fact, this is entirely due to the special nature of theString class. We know that it is immutable and final. At this time, param1= "Xiao Zhang"; in the function will actually create a new String object implicitly, and at the same time heap A new memory space will be opened in the memory, and param1 points to this newly opened memory space. There is no change in the data in the heap memory space pointed to by the original address str.
The above is the detailed content of Why is value only passed in Java?. For more information, please follow other related articles on the PHP Chinese website!