String str=new String("JAVA");
System.out.println("value of str before "+str); //JAVA
String str2=null;
str=str2;
System.out.println("value of str"+str); //null
System.out.println("value of str2"+str2);//null
str="Prog";
System.out.println("value of str"+str); //prog
System.out.println("value of str2"+str2);//null
问题 1 如果字符串是不可变的,为什么 str 的值会改变?
Student stu= new Student(123);
System.out.println("value of stu before "+stu); //some address is printed
Student stu2=null;
stu=stu2;
System.out.println("value of stu"+stu); //null
System.out.println("value of stu2"+stu2);//null
Student stu3=new Student(456);
stu=stu3;
System.out.println("value of stu"+stu); //some new address
System.out.println("value of stu2"+stu2);//null
问题 2.String 和 Object 的行为方式相同。那么为什么 String 是不可变的,而 Object 是可变的。区别在哪里