This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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 email@example.com
Joined: Nov 30, 2000
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?
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?
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
Joined: Apr 05, 2000
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
Joined: Nov 30, 2000
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
This doesn't deserve a new thread so I thought I'd ask, what exactly a "string literal"? Thanks
Joined: Sep 20, 2000
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 firstname.lastname@example.org [This message has been edited by Noah Carroll (edited December 07, 2000).]