File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on strings

 
Sham Grandhe
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Phil Rhodes
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tushar Gorde
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will also want to watch out for things like this:


The first will print abc, the second will print 123456
 
Sham Grandhe
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends,
thank you all for the kind response
shyam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic