Home  >  Article  >  Java  >  Parameter passing in java (only value passing, no reference passing)

Parameter passing in java (only value passing, no reference passing)

(*-*)浩
(*-*)浩forward
2019-08-17 16:30:562882browse

Why is it possible to change the attribute value of a reference variable? Please see the answer below.

Parameter passing in java (only value passing, no reference passing)

Data types in java

Data types in Java are divided into two categories: basic types and reference types. Correspondingly, variables are also divided into two types: basic types and reference types.

The basic type variable saves the original value, that is, the value it represents is the value itself;

The value saved by the reference type variable is the reference value, and the "reference value" points to the address of the memory space , represents a reference to an object, rather than the object itself. The object itself is stored at the address represented by the reference value.

Basic types include: byte, short, int, long, char, float, double, Boolean, returnAddress.

Reference types include: classes, interface types and arrays.

There is only value transfer in java

In daily coding, you will often see the following phenomenon:

1. For basic type parameters, in the method Reassigning parameters within the body will not change the value of the original variable.

2. For reference type parameters, re-assigning a reference to the parameter in the method body will not change the reference held by the original variable.

3. The parameters are operated within the method body without changing the value of the original variable.

4. For reference type parameters, the method body operates on the properties of the object pointed to by the parameter, which will change the property value of the object pointed to by the original variable.

For example:

public class Main {
    private static void getMiddleOne(boolean b, Boolean boo, Boolean[] arr){
        b = true;
        boo = new Boolean(true);
        arr[0] = true;
    }
       //测试
    public static void main(String[] args) {
        boolean b = false;
        Boolean boo = new Boolean(false);
        Boolean[] arr = new Boolean[]{false};
        getMiddleOne(b, boo, arr);
        System.out.println(b);
        System.out.println(boo.toString());
        System.out.println(arr[0]);
        /**
        * output:
        * false
        * false
        * true
        */
    }
}

We can answer the above phenomenon as long as we understand the following two points:

1. Basic data types The value is the value itself, so the value of b in the example is false; because the wrapper class will automatically box and unbox, it can be processed in the same way as the basic type, so the value of boo in the example is false; the array is a reference type, so arr The value is a reference to the Boolean[].

2. In Java, there is only value transfer and no reference transfer, so the three parameters passed into the getMiddleOne method are the value copy of b, the value copy of boo, and the value copy of arr.

It will be clear from the above two points. b=true and boo = new Boolean(true) executed in the getMiddleOne method assign new values ​​to their copies, so the value of the original variable is not changed. ; Similarly, arr[0] = true copies true to the first element of the array pointed to by the copy of arr. The value of arr and the value of the copy of arr are both references to the array, so the copy of arr points to The array and the array pointed to by arr are the same, so changing the elements of the array copied by arr will also affect the original variable arr.

Summary

In Java, only value is passed. Basic types pass a copy of the value, and reference types pass a copy of the reference.

The above is the detailed content of Parameter passing in java (only value passing, no reference passing). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete