----- JAVA based method parameters and return values are passed

The method of transmission parameters and return values ​​(Basic type)

public class Text {
    public int  changeNum(int a){
        System.out.println("方法执行之前"+a);
        a=10;
        System.out.println("方法执行之后"+a);
        return a;//返回值 将a临时变量空间内的值(引用)返回出来
    }
    


    public static void main(String[] args) {
        Text t=new Text();//堆内存中开辟空间
        int x=1;
        x=t.changeNum(x);//返回值把10抛出来 x接受
        System.out.println("方法执行完毕,main方法中a的值"+x);
    }
}

The method of transmission parameters and return values ​​(reference type)

public class Text {
   
    public void chnangeArray(int[] x){
        System.out.println("方法执行之前"+x[0]);
        x[0]=10;
        System.out.println("方法执行之后"+x[0]);

    }


    public static void main(String[] args) {
        Text t=new Text();
        int a[]=new int[]{0,1,2,3};
        t.chnangeArray(a);
        /方法存在堆内存里 ,方法执行在栈内存中的临时空间
        //调用方法时将a的值传递给了x  int[] x=a; 传递过来的就是一个引用
        System.out.println("方法执行完毕,main方法中a的值"+a[0]);
    }
}

And parameter arguments

  • Parameter space will be appreciated that the temporary variable x is a method of performing
  • It will be appreciated arguments passed in the argument when calling a method
  • Will arguments passed to the method invocation parameter content
  • If the content type is the basic parameter value passed is the same argument change
  • If the content is a reference type reference parameter is passed argument change with change

Guess you like

Origin www.cnblogs.com/CGGG/p/12556404.html