Ques 1 If string is immutable why is value of str changing??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
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
Ques 2.String and Object are behaving in same way .Then why String is immutable and Object mutable. Where's the difference