Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help

 
wang hong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

... 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic