『Java』Modify the difference between basic data types and reference data types

  • "Java" Modification of basic data types and reference data types bring the difference. When
    basic data types are passed, a new space is created in the stack to copy a copy of age. The modified age is a copy and will not modify the age value of the stack in the main function.
    Modifying the reference data type changes the data in the heap, and the arr in the stack and the copy in the main function are pointers, and they all point to the same data field in the heap. After the copy is changed, the number in the main function will also change.

7. The running result of the following code is

public static void main(String[] args) {
    
    
    int arr[] = {
    
    1, 3, 5, 7, 9};
    int num = 10;
    showArray(arr, num);
    System.out.println("arr[2]的结果是:"+arr[2]);
    System.out.println("num的结果是:"+num);
}
private static void showArray(int[] arr, int num) {
    
    
    arr[2] = 6;
    num = 1;
}

In the
end of java , the result of arr[2] is: 6. The result of num is: 10.


arr[2]的结果是:6
num的结果是:10

Guess you like

Origin blog.csdn.net/codemokey/article/details/106060712