Let’s start by reviewing some of the technical terms used in programming languages for passing parameters to methods (or functions).

Call by value: Indicates that a method receives a value provided by the caller.

Call by reference: Indicates that the method receives the address of the variable provided by the caller.

A method can change the value of the variable that corresponds to a pass-reference, but not to a pass-value call. It is used to describe how method parameters are passed in various programming languages (not just Java).

The Java programming language has always adopted call-by-value. That is, a method gets a copy of all the parameter values, that is, it cannot modify the contents of any parameter variables passed to it.

Here are three examples to illustrate

example 1

public static void main(String[] args) {
    int num1 = 10;
    int num2 = 20;
    swap(num1, num2);
    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
}

public static void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
}
Copy the code

Results:

a = 20 b = 10 num1 = 10 num2 = 20

Num1 and NUM2 are not affected when a and B are swapped. The values in a and b are copied from num1 and num2. Num1, num2, num1, num2, num1, NUM2, num1, NUM2, NUM1, NUM2

From the example above, we have seen that a method cannot modify the parameters of a primitive data type, whereas object references are different as parameters, see example2.

example 2

public static void main(String[] args) {
    int[] arr = {1.2.3.4.5};
    System.out.println(arr[0]);
    change(arr);
    System.out.println(arr[0]);
}

public static void change(int[] array) {
    // Change the first element of the array to 0
    array[0] = 0;
}
Copy the code

Results:

1 0

Resolution:

Array is initialized and a copy of arR is a reference to an object, which means that array and ARR refer to the same array object. Therefore, external changes to the referenced object are reflected in the corresponding object.

As we have seen with Example2, it is not difficult to implement a method that changes the state of an object’s parameters. The reason is simple: the method gets a copy of the object reference, and the object reference and other copies reference the same object at the same time. Many programming languages (in particular, C++ and Pascal) provide two ways to pass parameters: value calls and reference calls.

Some programmers (and even the author of this book) think that the Java programming language makes referential calls to objects. Since this misconception is somewhat common, a counter example is given to illustrate the problem in detail.

example 3

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student s1 = new Student("Zhang");
    Student s2 = new Student("Xiao li");
    Test.swap(s1, s2);
    System.out.println("s1:" + s1.getName());
    System.out.println("s2:" + s2.getName());
}

public static void swap(Student x, Student y) {
    Student temp = x;
    x = y;
    y = temp;
    System.out.println("x:" + x.getName());
    System.out.println("y:" + y.getName());
}
Copy the code

Results:

X: Xiao Li Y: Xiao Zhang S1: Xiao Zhang S2: Xiao Li

Before the exchange:

After the exchange:

It is clear from the above two figures that the method does not change the object references stored in the variables S1 and S2. The arguments x and y to the swap method are initialized as copies of two object references that this method swaps.

The Java programming language does not make reference calls to objects; in fact, object references are passed by value. To summarize the usage of method parameters in Java:

  1. A method cannot modify the parameters of an elementary data type (that is, numeric or Boolean).
  2. A method can change the state of an object parameter.
  3. A method cannot have an object parameter reference to a new object.

Reference: Section 4.5, Chapter 4, Fundamentals, 10th edition, Java Core Technology Volume I