• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String question...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no! that is true once a string is created it cannot be changed. you can reassign the reference to point at a different string but you cannot change the string object itself.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thumb rule : Basically none of the operations can modify the actual String Object.
A. s.append("aaa");

String does not have this method. Hence will throw error

B. s.trim();
Even though this is a valid String method, it returns a new String Object


C. s.substring(3);
Even though this is a valid String method, it returns a new String Object



D. s.replace('z', 'a');
Even though this is a valid String method, it returns a new String Object


E. s.concat(s);
Even though this is a valid String method, it returns a new String Object


 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question simply says 'modify' or apply to String type.
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
sanjay kanungo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic