值传递与引用传递

值传递

public class Demo03 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);

        Demo03.change(a);
        System.out.println(a);
    }

    public static void change(int a){
        a=10;
    }
}

两次输出的结果均为1

引用传递

public class Demo02 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);

        Demo02.change(person);
        System.out.println(person.name);

    }
    public static void change(Person person){
        person.name="theshy";
    }
}

class Person{
    String name;
}

第一次输出结果为null 第二次为theshy

posted @ 2021-05-23 21:01  Theshy-Riven  阅读(24)  评论(0)    收藏  举报