The moose likes Beginning Java and the fly likes strings are immutable!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "strings are immutable!!" Watch "strings are immutable!!" New topic
Author

strings are immutable!!

Harsha Vardhan Madiraju
Greenhorn

Joined: Dec 19, 2003
Posts: 24
We say Strings are immutable but the following code is working fine, pls justify
String s="Java";
s=s+"Technology";
System.out.pritnln(s);
m bhramaresh
Greenhorn

Joined: Jan 09, 2004
Posts: 13
hi,
just go through this url, this may help u
http://www.churchillobjects.com/c/11027.html
bhramaresh
Ruben Steins
Greenhorn

Joined: Nov 22, 2002
Posts: 15


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.
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strings are immutable!!
 
Similar Threads
immutable Strings
Strings and StringBuffers
Strings are immutrable?
String
String replace???(Urgent)