s is a reference to the String "Java" at first, and becomes a reference to a brand new String "JavaTechnology". The first String will become garbage (provided no other references to it exist), but remains unchanged, hence the immutable.
Joined: Feb 03, 2003
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
If you want to become a rich, do not work for others but make others to work for you.