My code is :
String str = "Hello";
String str1;
str1 = str;
str1 = str1 + " World";
System.out.println(str1);
System.out.println(str);
The output I get is :
Hello World
Hello
The output I was expecting is Hello World for both the cases because according to my understanding after str1 = str both objects are referencing to same location so if I change the content of one object other should also get affected.
So, is str1 = str1 + " World"; creating a new string object at different memory loction.?