aspose file tools*
The moose likes Beginning Java and the fly likes immutability of strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "immutability of strings" Watch "immutability of strings" New topic
Author

immutability of strings

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
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

Joined: Sep 20, 2000
Posts: 96
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

Joined: Nov 30, 2000
Posts: 111
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

Joined: Oct 11, 2000
Posts: 83
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?


Allen
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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

Joined: Nov 30, 2000
Posts: 111
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

Joined: Nov 05, 2000
Posts: 3
This doesn't deserve a new thread so I thought I'd ask, what exactly a "string literal"?
Thanks
Noah Carroll
Ranch Hand

Joined: Sep 20, 2000
Posts: 96
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

Joined: Dec 11, 2000
Posts: 6
Interesting thread.
Let me get this clear:
strings are immutable, but Strings aren't?
Neeraj Thakkar
Ranch Hand

Joined: Dec 06, 2000
Posts: 80
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.

Neeraj Thakkar<br />"Try to get what you like,Else you will be forced to like what you get."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: immutability of strings