Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String question...

 
sanjay kanungo
Greenhorn
Posts: 7
  • 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?
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
 
Randall Twede
Ranch Hand
Posts: 4482
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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.
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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


 
David Harrigan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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).]
 
Kirti Dhingra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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
 
jay vyas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic