Originally posted by Harsha Vardhan Madiraju:
We say Strings are immutable but the following code is working fine, pls justify
String s="Java";
s=s+"Technology";
System.out.pritnln(s);
String s="Java";
is internally converted to,
String s = new String("Java");
So here new object of String class is being created and reference is
assigned to variable 's';
s=s+"Technology";
Here new object is being created and reference is being assigned to
variable 's' and thus reference to Object '
Java' is overwritten.
Immutable means we can not change content of object but can change
reference assigned to it.
For more info, check this link.
http://www.csc.calpoly.edu/~csc101/studynotes/Strings.htm