Why elements in the array can be adjusted to change the numerical methods, local variables may not be changed by the adjusting element method

import java.util.Scanner;

public class 交换树 {
    public static void main(String[] args) {
        /*System.out.println("输入两个数:");
        Scanner scanner=new Scanner(System.in);
        int a=scanner.nextInt();
        int b=scanner.nextInt();*/
        int a=1;
        int b=2;
        jiaohuan(a ,b);
        System.out.println(a+","+b);
    }
    public static void jiaohuan(int num1,int num2){
        System.out.println(num1+","+num2);
        int temp;
        temp=num1;
        num1=num2;
        num2=temp;
        System.out.println(num1+","+num2);

    }
}
1,2
2,1
1,2
public static void main(String[] args) {

    int a = 10;
    int b = 20;
    System.out.println("a: " + a + ",b: " + b);
    change(a,b);
    System.out.println("a: " + a + ",b: " + b);

    int[] arr = {1,2,3,4,5};
    change(arr);
    System.out.println(arr[1]);
}

public static void change(int a,int b)  {

    System.out.println("a: " + a + ",b: " + b);
    a = b;
    b = a + b;
    System.out.println("a: " + a + ",b: " + b);
}

public static void change(int[] arr){
    for(int x = 0 ; x < arr.length ; x++){
        if(arr[x]%2 == 0){
            arr[x] *= 2;
        }
    }
}
1020
1020
2010
1020
4

Careful observation above two codes, modulation method will find all, elements of the array can be adjusted to change the numerical methods, local variables may not be changed by the adjusting element method, tangled for a long time , finally found, is stored in the stack because the array , local variables are stored in the stack, the stack can be changed by the process of

Published 43 original articles · won praise 7 · views 1750

Guess you like

Origin blog.csdn.net/y18791050779/article/details/104110549