一 对象的引用
1.
Student stu1=new Student("小明"); //stu1指向新创建的堆内存空间
Student stu2=null; //只在栈中创建变量stu2
stu2=stu1; //stu2引用stu1,stu1和stu2都指向同一个堆内存地址
stu2.name="小红";
System.out.println(stu1.name);//就会输出“小红”
2.
Student stu1=new Student(); //stu1指向新创建的堆内存空间
Student stu2=new Student(); //stu2指向另一个新创建的堆内存空间
stu2=stu1; //stu2引用stu1,stu1和stu2都指向同一个堆内存地址,
//stu2之前指向的堆内存空间被Gc回收
二 字符串的应用
String st1="小白";//st1指向堆中的"小白"对象
String st2=null;
st2=st1; //st2引用st1,st2也指向堆中的"小白"对象
st2="小黑"; //st2指向堆中新创建的"小黑"对象,st1不变,还是指向堆中的"小白"对象