• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Immutability  RSS feed

 
siva krishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I often find mention of Strings being immutable...I still haven't been able to interpret it...I mean if I declare a String :
//
String s1="something";
I can always change the String contents with another assignment can't i ??
I know I am missing a point here...pls explain.
Thanks,
Siva
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can always change the String contents with another assignment can't i ??
Nope. You change the value of the reference to the String object. For example:

What happens here is, on the first statement the compiler creates a String object with "abc" and assigns the address (more or less) of that object to s1. The next statement just copies the content of s1 to s2. The final statement does exactly the same thing as the first, creates a new String object with contetnts "def", and assigns its address to s1. So what is the contents of the String object referenced by s2? It's "abc", proving that Strings are immutable.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi siva
String is an immutable class,which means that once a String object is created,we can not change it.
The method of the String class,such as toUpperCase do not modify the original string;they return a new string.
This is Java adopts this immutablility restriction to implement an efficient memory allocation scheme for managing string object.
If it is not clear,please post a reply
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:
[b]next statement just copies the content of s1 to s2.

AFAIK, the Strings are not copied during such an assignment. Instead, both references point to the same object.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LL: ...the Strings are not copied during such an assignment. Instead, both references point to the same object

MM: ... and assigns the address (more or less) of that object to s1. The next statement just copies the content of s1 to s2.

Did I say that the Strings were copied? The content of s1 is not the String itself but the heap location of the String object.
 
siva krishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys...got the point now.
What kind of questions can we expect in the scjp that test us on this aspect
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of questions can we expect in the scjp that test us on this aspect
You can expect questions like:
Given the following code segment, what will the contents of s1 be?

A. "abc"
B. "def"
C. An ImmubtableException will be thrown at line 3
D. It is impossible to tell from the above code.

What is the result of the following code segment?

A. s1 == s2 will be printed to standard output.
B. s1 equals s2 will be printed to standard output.
C. s1 != s2 will be printed to standard output.
D. The code will not compile.
[ May 10, 2003: Message edited by: Michael Morris ]
 
siva krishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!