In Java, method parameters are passed by value. If the parameter is of a basic data type, the parameter is passed the data value of the argument. If the parameter is a reference data type, the parameter is passed the address value of the argument.

  1. Parameters are basic data types
main(){
    int m=10;
    int n=20;
    swap(m,n);
    System.out.println(m,n);
    }
swap(int m,int n){
    int temp=m;
    m=n;
    n=temp;
    }
Copy the code

The above method swaps m, n in swap. The method is destroyed when it ends. So system.out.println (m,n) statements output the same m,n in main(). 2. Parameters are reference data types

main(){ Data data=new Data(); data.m=10; data.n=20; The new class. Swap (data); System.out.println(m,n); } swap(Data data){ int temp=data.m; data.m=data.n; data.n=temp; } class Data(){ int m; int n; }Copy the code

If the object is not destroyed, the argument refers to the Data object after swap, then m and n are successfully swapped.