wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question on strings " Watch "question on strings " New topic
Author

question on strings

Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
hai friends,
this is a javacertificate how is the question correct plz explain me.
Which of the following method calls will change the contents of the String str? String str = new String("Hello World");

1str.trim();
2str.toLowerCase();
3str.toUpperCase();
4str.replace('H','C');
5none of the above
the answer is 5 none how is this please explain me
thanking u
shyam
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,
String objects are immutable, which means that everytime
you perform an operation on a String a new String instance
is 'secretly' generated in the so-called String constant
pool.
Therefore none of the answers 1 to 4 will actually change
the original String object.
Greetings,
Gian Franco


"Eppur si muove!"
Phil Rhodes
Ranch Hand

Joined: Dec 27, 2003
Posts: 65
String in Java are immutable. Once created, a string can never be changed.
So, when you see code like this:
String a = "foo";
String b = " bar";
a = a + b;
System.out.println( a );
and it prints "foo bar", how does it work if strings are immutable? It works by creating a NEW string, which contains "foo bar" and assigning it to the String reference variable a. Note, it's the string itself which cannot be modified... the reference can be changed to point to a new String.
So, when you use any of the String functions like trim(), they return a NEW String object, which reflects the changes. If you want to use that "modified" String, you have to capture it with a variable. So, do this:
String a = " 123 ";
String b = a.trim();
System.out.println( b );
will print the trim()'d String.
If you want a Stringlike object that CAN be modified, investigate class StringBuffer. Understand the difference between how String and StringBuffer work, because it will definitely be on the exam.


A+, Network+, SCJP, SCWCD<br />preparing for SCBCD, SCEA, CompTIA I-Net+
Tushar Gorde
Greenhorn

Joined: Jan 30, 2004
Posts: 3
Hey Dear...The only method which can change the contents is replace()..but this method is of StringBuffer & not of String class...So option 5th none of the above is correct...All other option is not changing the state String object.
Originally posted by kumar shyam:
hai friends,
this is a javacertificate how is the question correct plz explain me.
Which of the following method calls will change the contents of the String str? String str = new String("Hello World");

1str.trim();
2str.toLowerCase();
3str.toUpperCase();
4str.replace('H','C');
5none of the above
the answer is 5 none how is this please explain me
thanking u
shyam
Julie Hay
Greenhorn

Joined: Nov 26, 2003
Posts: 4
You will also want to watch out for things like this:


The first will print abc, the second will print 123456


Julie<br />Dream a little dream...Then go get it. Little dreams make a BIG dream a reality.
Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
hai friends,
thank you all for the kind response
shyam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on strings
 
Similar Threads
Referenec passing
Objects- references
Can Constructor be synchronized? Why?
compare() method of Comparator
a question on creating string object.