[Java Interview] Are method parameters passed by value or by reference in Java?

Is Java pass-by-value or pass-by-reference? - Hollis' answer -
Is it passed by value or by reference when calling a method in Java?

Combining the two articles, it is clear~

The concept of passing by value and passing by reference should be clarified first:

Passing by value: refers to copying the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.
Pass by reference: It means that the address of the actual parameter is passed to the function when the function is called, then the modification of the parameter in the function will affect the actual parameter.

insert image description here

To sum up,
method parameters in java are passed by value, and there is no pass by reference.
If the argument is a primitive type, a copy of the literal value of the primitive type is passed.
If the parameter is a reference type, what is passed is a copy of the address value of the object referenced by the parameter in the heap.

Guess you like

Origin blog.csdn.net/huhuhutony/article/details/120816486