基本数据类型的传值:

package com.imooc.method;

public class ExchangeDemo1 {
    public void add(int n) {
        n++;
        System.out.println("方法中n的值:"+n);
    }
    public static void main(String[] args) {
        int n=10;
        System.out.println("方法前n的值:"+n);
        ExchangeDemo1 ed1=new ExchangeDemo1();
        ed1.add(n);
        System.out.println("方法后n的值:"+n);
    }

}
package com.imooc.method;

public class ExchangeDemo {
    public void swap(int a,int b) {
        int temp;
        System.out.println("交换前:a="+a+",b="+b);
        temp=a;a=b;b=temp;
        System.out.println("交换后:a="+a+",b="+b);
    } 
    public void swapTest() {
        int m=4,n=5;
        System.out.println("交换前:m="+m+",n="+n);
        swap(m,n);
        System.out.println("交换前:m="+m+",n="+n);
    }
    public static void main(String[] args) {
        //方法中传值:
        ExchangeDemo ed=new ExchangeDemo();
        ed.swapTest();

    }

}

方法中基本数据传值是,基本数据类型的传值,方法中修改数据之后,不会影响到主方法中的数据值。

原文地址:https://www.cnblogs.com/yiweiyihang/p/11980609.html