Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

question on strings

 
Ranch Hand
Posts: 73
  • 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
 
blacksmith
Posts: 979
  • 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
 
Ranch Hand
Posts: 65
  • 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.
 
Greenhorn
Posts: 3
  • 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

 
Greenhorn
Posts: 4
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends,
thank you all for the kind response
shyam
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic