java 值传递的例子

da家都知道java传参是形参,即值传递。下面写了几个例子,加深一下了解。

1.基本类型 boolean/Boolean:

  public static void main(String[] args) {
        //小写的boolean,在方法中被修改为反值的情况
        boolean a = false;
        checkBoolean(a);
        System.out.println(a);
        //大写的Boolean被修改
        Boolean a1 = new Boolean("false");
        checkBoolean(a1);
        System.out.println(a1);
        checkBoolean1(a1);
        System.out.println(a1);
        checkBoolean2(a1);
        System.out.println(a1);
    }

    public static void checkBoolean(boolean a){
        a = !a;
        System.out.println("checkBoolean:"+ a);
    }

    public static void checkBoolean1(Boolean a){
        a = true;
        System.out.println("checkBoolean:"+ a);
    }

    public static void checkBoolean2(Boolean a){
        a = new Boolean("true");
        System.out.println("checkBoolean:"+ a);
    }
View Code

输出结果:

checkBoolean:true
false
checkBoolean:true
false
checkBoolean:true
false
checkBoolean:true
false
View Code

现象:

方法里修改值,并不影响方法外面的值 。

2.基本类型int/Integer

 public static void main(String[] args) {
        //小写的int,在方法中被修改的情况
        int a = 1;
        checkInt(a);
        System.out.println(a);
        //大写的Integer被修改
        Integer a1 = new Integer("1");
        checkInt(a1);
        System.out.println(a1);
        checkInteger(a1);
        System.out.println(a1);
        checkInteger1(a1);
        System.out.println(a1);
        checkInteger2(a1);
        System.out.println(a1);
    }

    public static void checkInt(int a){
        a = 2;
        System.out.println("checkInt:"+ a);
    }

    public static void checkInteger(Integer a){
        a = 2;
        System.out.println("checkInteger:"+ a);
    }

    public static void checkInteger1(Integer a){
        a = new Integer("2");
        System.out.println("checkInteger1:"+ a);
    }

    public static void checkInteger2(Integer a){
        a = new Integer("2526");
        System.out.println("checkInteger2:"+ a);
    }
View Code

输出结果:

checkInt:2
1
checkInt:2
1
checkInteger:2
1
checkInteger1:2
1
checkInteger2:2526
1
View Code

现象:

方法里修改值,并不影响方法外面的值 。

3.引用类型

  public static void main(String[] args) {
        //小写的int,在方法中被修改的情况
        List<String> a = new ArrayList<>();
        a.add("a");
        a.add("b");
        a.add("c");
        //修改值
        checkList(a);
        System.out.println(a);
        //指向新地址或者赋null
        checkList1(a);
        System.out.println(a);
        checkList2(a);
        System.out.println(a);
        checkList3(a);
        System.out.println(a);
    }

    public static void checkList(List a){
        a.add("1");
        a.add("2");
        System.out.println("checkList:"+ a);
    }

    public static void checkList1(List a){
        List<String> b = new ArrayList<>();
        b.add("1");
        b.add("2");
        a = b;
        System.out.println("checkList1:"+ a);
    }

    public static void checkList2(List a){
       a = null;
        System.out.println("checkList1:"+ a);
    }

    public static void checkList3(List a){
        a.clear();
        System.out.println("checkList1:"+ a);
    }
View Code

输出结果:

checkList:[a, b, c, 1, 2]
[a, b, c, 1, 2]
checkList1:[1, 2]
[a, b, c, 1, 2]
checkList1:null
[a, b, c, 1, 2]
checkList1:[]
[]
View Code

现象:

修改值时,方法内修改的,方法外的对象也会改变;

指向新地址(new、null)的变化,不会影响方法外的值;

原文地址:https://www.cnblogs.com/wmg92/p/13425837.html