Is Java pass by reference or value?

The way of passing in Java has always been a point of confusion. Many people think that Java is passed by value, and some people insist that Java is passed by reference. In fact, Java adopts the method of passing by value, but for variables of reference type, its value is a reference to an object , which also leads to some misunderstandings.

first:

Value transfer refers to copying the value of the actual parameter to the formal parameter when the method is called, that is, the modification of the formal parameter inside the method will not affect the value of the actual parameter. This means that the modification of the formal parameters in the method only affects the local variables inside the method, and will not affect the variables outside the method.

Passing by reference refers to copying the reference (address) of the actual parameter to the formal parameter when the method is called. The formal parameter and the actual parameter point to the same memory address, so the modification of the formal parameter will affect the value of the actual parameter.

In Java, basic data types (such as int, float, boolean, etc.) are passed by value, and reference types (such as arrays, objects) are also passed by value, but it should be noted that the value of a reference type is an object quote.

Secondly:

Let's look at the following example:

public class Main {
    public static void main(String[] args) {
        int number = 10;
        System.out.println("Before method call: " + number);
        modifyValue(number);
        System.out.println("After method call: " + number);
    }
    
    public static void modifyValue(int value) {
        value = 20;
    }
}

In the above code, we defined an integer variable numberand assigned a value of 10. Then we called modifyValuethe method and numberpassed valuethe formal parameters as actual parameters. Inside modifyValuethe method, we modified valuethe value of 20. Then we print out numberthe value before and after the method call.

The output is as follows:

Before method call: 10
After method call: 10

It can be seen that although the value of the value is modified inside the method value, the value of the value is not affected number. This shows that the basic data types in Java are passed by value, and the modification of the formal parameters inside the method will not affect the actual parameters.

Next, let's use an example to illustrate the way of passing reference types in Java:

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 3};
        System.out.println("Before method call: " + array[0]);
        modifyArray(array);
        System.out.println("After method call: " + array[0]);
    }
    
    public static void modifyArray(int[] arr) {
        arr[0] = 100;
        arr = new int[]{4, 5, 6};
    }
}

In the above code, we defined an array of integers arrayand initialized to {1, 2, 3}. Then we called modifyArraythe method and arraypassed arrthe formal parameters as actual parameters. Inside modifyArraythe method, we first arrmodify the first element of to 100, and then arrreassign it to a new array {4, 5, 6}. Then we print out array[0]the value before and after the method call.

The output is as follows:

Before method call: 1
After method call: 100

arrIt can be seen that although the first element modified inside the method is 100, this modification also arrayaffects array[0]the value of 100. This shows that reference types in Java are passed by value, but for a variable of a reference type, its value is a reference to an object. Therefore, the state of the pointed-to object can be modified by the reference, and this modification can be seen outside the method.

However, after modifyArrayreassigning arra new array inside the method, it will not affect the outside of the method array. This is because inside the method, arrthe reference points to a new array object, but this change does not affect arraythe reference outside the method, which still points to the original array object.

To sum up, the way of passing in Java is pass by value. For basic data types, the modification of the formal parameters inside the method will not affect the actual parameters; while for the reference type, the modification of the formal parameters inside the method can affect the state of the referenced object, but reassigning the formal parameters will not Affects the object referenced by the actual parameter.

Dark Horse Programmer Java Zero-Basic Video Tutorial_Part 1 (Introduction to Java, including Stanford University Exercises + Likou Algorithm Questions and Dachang Java Interview Questions) Dark Horse Programmer
Java Zero-Basic Video Tutorial_Part 2 (Java Introduction, including Stanford University Exercises + Likou algorithm questions and Dachang java interview questions)
 

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/132401959