值传递 和 引用传递

值传递 和 引用传递

public class Demo5 {
    public static void main(String[] args) {
        //值传递
        int a = 1;
        change(a);
        System.out.println("a="+a);//a=1
    }
    // static 修饰的方法,随着类的加载而加载,随着类的消亡而消亡
    // 故本类里,用static修饰的方法里不能直接调用非static修饰的方法,因为其还没加载
    // 非static的方法可以直接调用本类里的任何方法
    public static void change(int a){
        a = 10;
    }
}


public class Demo6 {
    public static void main(String[] args) {
        //引用传递
        Student student = new Student();
        System.out.println(student.name);//null
        change(student);
        System.out.println(student.name);//小明
    }

    public static void change(Student student){
        student.name = "小明";
    }
}
class Student {
    String name;
}
posted @ 2020-11-15 19:00  阿灿呀  阅读(107)  评论(0)    收藏  举报