aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String question..." Watch "String question..." New topic
Author

String question...

sanjay kanungo
Greenhorn

Joined: Dec 06, 2000
Posts: 7
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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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.


SCJP
Visit my download page
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198

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

Joined: Dec 12, 2000
Posts: 52
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

Joined: Dec 07, 2000
Posts: 133
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

Joined: Dec 13, 2000
Posts: 32
The question simply says 'modify' or apply to String type.
Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
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

Joined: Nov 14, 2000
Posts: 104
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>
jay vyas
Greenhorn

Joined: Dec 12, 2000
Posts: 9

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

Joined: Dec 06, 2000
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String question...
 
Similar Threads
String Creation Question, don't understand :(
Mock Exam HF EJB
How many String objects will be created when this method is invoked?
instance variables
Strings Doubt