• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

immutability of strings

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am referring to a ques. from marcus greeen tutorial that says--
You have created two strings containing names. Thus
String fname="John";
String lname="String"
How can you go about changing these strings to take new values within the same block of code?
1)
fname="Fred";
lname="Jones";
2)
String fname=new String("Fred");
String lname=new String("Jones");
3)
StringBuffer fname=new StringBuffer(fname);
StringBuffer lname=new StringBuffer(lname);

4) None of the above
Acc to me the ans should be 1) but the tutorial says it is 4)
anybody please explain
thanks
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not possible to change a string no matter what code block you are in. Strings are immutable(can't be changed). when you have code such as

it DOES NOT CHANGE the original string. It only makes a new memory location and places the value there. The original memory location assigned to myname is now available for garbage collection i believe because it can no longer be accessed through the myname variable.
------------------
I hope its helps, feel free to email me noahcarroll@juno.com
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but noah , the ques does not ask about the previous memory locations rather it asks
How can you go about changing these strings to take new values within the same block of code?

which i suppose can be changed by just assigning them new values no matter if they take new addresses ,isn't it?

 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preeti,
The issue of a string being immutable is typically difficult for a new Java programmer to grasp, especially when it appears that a simple assignment statement changes the value of the string. However, one of the key concepts that comes into play is the idea that a "string" is not a "String".
A "string" (not capitalized) is just a series of characters. However, a "String" (capitalized) is a Java object.
If you recall, an object encapsulates data and methods. In a String object, the encapsulated data are a series of characters...a string. But--and this is important--you can't get into that String object to change the series of characters of the data string. You can get the series of letters (the encapsulated string) through methods that are in the String object, and you can get a sub-set of the encapsulated string by methods in the String ojbect. But there is no method in the String object that allows you to change the string data that is encapsulated in the String object.
Like I said in the start, this is not an easy concept for beginners to grasp, but it is fundamental.
Although code such as the examples you displayed in your original posting might suggest that strings are being changed, what is really happening is that the references (the variables such as fname and lname) to objects are changing what they refer to...old String objects are being discarded and new objects are being assigned to the same references. And this is done by having the references (fname and lname) point to different areas of memory!
The fact that the original String objects have been discarded and replaced by new String objects means you haven't changed the encapsulated string data of your original String objects...you have discarded them (the original String objects) and replaced them with new String objects.
As Noah wrote, "strings are immutable."
If you'd like to read more about immutable objects, check out the free download of Bruce Eckel's book, "Thinking In Java" at http://www.bruceeckel.com Look at pages 1052+.
Now, have I made it as clear as mud?
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fname and lname are handles to the actual String objects. You can change what the handles "point" at but the underlying objects are not changed.
So you didn't change the String objects.
John
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preeti,
You may want to post questions like this (and your other recent post) over in the Programmer Certification Study forum since they seem very much along those lines, too.
But just post in one forum, not both! :-)
John
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all especially allen for taking time to explain it so nicely.
john ,i will definitely remember to classify my posts into these forums as u stated
thanks again
preeti
 
Josh Garrett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't deserve a new thread so I thought I'd ask, what exactly a "string literal"?
Thanks
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a string literal is just something that falls between two quote marks
"i am a string literal"
------------------
I hope its helps, feel free to email me noahcarroll@juno.com
[This message has been edited by Noah Carroll (edited December 07, 2000).]
 
E
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting thread.
Let me get this clear:
strings are immutable, but Strings aren't?
 
Neeraj Thakkar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ED,
You said that "string" are immutable but "String" are not. But actually they are vice versa "string" are mutable but "String" are immutable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic