| Author |
help
|
wang hong
Greenhorn
Joined: Apr 08, 2003
Posts: 3
|
|
Given a string constructed by calling s = new String(“xyzzy” ) , which of the calls listed below modify the string? (Choose all that apply.) A. s.append(“aaa” ) ; B. s.trim(); C. s.substring(3); D. s.replace(‘z’, ‘a’ ) ; E. s.concat(s); F. None of the above why select "f". can anyone tell me? thx. Edited to try to get rid of winkies - Barry [ April 18, 2003: Message edited by: Barry Gaunt ]
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
None of the methods modify the original string, they all create new strings from the original string. It is very important to undestand that strings cannot be changed, they are said to be "immutable".
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Donald R. Cossitt
buckaroo
Ranch Hand
Joined: Jan 31, 2003
Posts: 401
|
|
That was a bit of a trick question though don't you think?
None of the methods modify the original string, they all create new strings from the original string.
If I put a CD palyer (in addition to the stock AM radio) in my otherwise 'stock' 1957 Chevy; did I 'modify' or 'change' it by creating something different from the original, than the original? Semantics I know, but it's those type of questions (on tests especially ) that seemingly are there merely to trip you up. doco
|
doco
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
... that seemingly are there merely to trip you up
I don't think that test designers are the evil deviants that many of us feel they are. Questions such as this one are designed to test your knowledge of immutability and understand its consequenses. Immutability for objects like Strings and the value wrappers such as Integer, Long etc. makes a lot of sense. Object identity in these cases makes little sense, so it is easy to cache those objects or implement the Flyweight pattern using them.
|
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
|
 |
 |
|
|
subject: help
|
|
|