Given a string constructed by calling s = new String("xyzzy"), which of the calls listed below modify the string? A. s.append("aaa"); B. s.trim(); C. s.substring(3); D. s.replace('z', 'a'); E. s.concat(s); My answer would be B, C, D, E. However the correct answer per RHE is none of the above because Strings are immutable. I think the question is ambiguous. Any thought? Thanks, Sanjay
Just to add my tupence's worth:- Everything said so far is correct, but to clarify it a bit further, consider this:- if you have a bit of code: String s = "ABCD"; then if one was to do:- s = s.concat("EFG"); Then the following can be observed:- s is indeed immutible, it cannot be changed once created. With the concat method a new object of type string is created with "ABCEFG" referenced. It is then assigned to the memory reference of s, in effect 's' now points to a different memory location (and the original "ABC" is gc'ed). David Harrigan
Originally posted by David Harrigan: ...snip... s is indeed immutible, it cannot be changed once created. With the concat method a new object of type string is created with "ABCEFG" referenced. It is then assigned to the memory reference of s, in effect 's' now points to a different memory location (and the original "ABC" is gc'ed). David Harrigan
The original "ABCD" won't be GC'ed because it wasn't created via a new call. It stays in memory.
[This message has been edited by Sam Wong (edited December 13, 2000).]
The question simply says 'modify' or apply to String type.
Joined: Dec 07, 2000
No, the question states which method will modify the String. String being "ABCD". Hence the answer being none of the above. The question is testing whether you realize that Strings are immutible and any operations to a String will only result in the creation of a new String and not alteration of the existing one.
Hi, The correct answer is "none of the above" as java.lang.String objects are immutable. However,all the other choices are applicable for java.lang.StringBuffer object as StringBuffer is "mutable" and hence can be modified. - Suresh Selvaraj
Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
Originally posted by sanjay kanungo: [B]Given a string constructed by calling s = new String("xyzzy"), which of the calls listed below modify the string?
hi sanjay, the correct answer is none of above. why? once u created a String u can not modify. strings r immutable and that u know. now consider the following code. s=new String("xyz"); String a=s.concat(s); String b=s.trim(); String c=s.substring(1,2); String d=s.replace('a', 'z'); here if u try to compile this code it will compile and run succesfully b'coz here we r creating a new String. hope this will help
Joined: Dec 06, 2000
Thanks for all the explanations. They are all very good. I guess, one should not interpret the phrase "modify the string" in the question as loosely as I was doing. Sanjay