• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I had a question from Strings that appeared in SHE mock exam:
Given a string s, constructed by calling s = new String("xyzzy"), which of the calls listed below modify the string represented by s? (Choose all that apply.)
a.) s.append("aaa");
b.) s.trim();
c.) s.replace('z', 'a');
d.) s.concat();
e.) s.substring('3')
-------------------------------------
Correct answer was given as: None
Explanation: Strings are immutable
If the Strings are immutable, why are the methods in the Strings class provided in the first place? Can anyone explain?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods on String class returns another String object.
For example in the code given below,
String s = " Hello";
String s1;
s1 = s.trim();
calling the method trim() on it will not change the object referenced by s but will return a different object which is being assigned to s1. So after execution, s1 will contain "Hello" where as s will still have " Hello".
Also, there can be a statement like,
s = s.trim();
In this also, s.trim() will return another String object and s is reassigned with the reference to the new object.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gautam,
If the value of String s = "Hello" (without any blank spaces), s=s.trim() will not create any new objects, right??
Correct me
Thanx
Vidya
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vidya, Gautam,
This is what the documentation says:


trim
public String trim()
Removes white space from both ends of this string.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.
[red]Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1). [/red]
This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.
Returns:
this string, with white space removed from the front and end.


That means that even if there is nothing to trim, a new string is created and returned.
Correct me if I got this wrong...
Shubhangi

[This message has been edited by Shubhangi A. Patkar (edited November 28, 2000).]
 
Ajit Deshpande2
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Gautam, Vidya and Shubhangi....
I have things much clearer now...Gautam your explanation was perfect...also thanx Vidya and Shubhangi to add more on the topic...
Ajit
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shubhangi,
Documentation as per u says "when a string doesn't have leading and trailing blank spaces...it returns reference to this string". then why do you say that a new object is created?
[This message has been edited by rajani peddi (edited November 28, 2000).]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubhangi A. Patkar:
Vidya, Gautam,
This is what the documentation says:
That means that even if there is nothing to trim, a new string is created and returned.
Correct me if I got this wrong...
Shubhangi

[This message has been edited by Shubhangi A. Patkar (edited November 28, 2000).]


Ranchers ,
listen to me ,
i have not read this post but on surfing i have met through a statement originally posted by Shubhangi .
i want to give my openion in this concern ( if helpfull to anyone of you collect it) according to above statement following code should print false , but i am sorry to say it is "not" .
class contradiction {
public static void main(String arg[]) {
String s = new String("govinda");
String a = s.trim();
System.out.println(a==s);
}
}
i want to say something here , beside of clearing out your doubt on this type of question here better all of you open the java api source code available free with jdk and try to find out there how a particular method performing its work. don't try to make analogies because they won't work here .
- take care
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable, meaning that you cannot change it. I think the common confusion is that the methods provided by Java. Here is my understanding:
If you use any of the methods without assigning them to the object reference, nothing happens.
For example:
String s = " hello ";
s.trim(); //s still holds the " hello "
String str = s.trim(); //str holds "hello"
s = s.trim(); //s is now referencing to a different object ("hello")
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a string function does nothing to the string, such as trim() or a replace that has nothing to replace, then a new string is NOT created. So as Govinda pointed out, when the trim() has nothing to trim, then a new string is not created so a==b. Add some spacing to the the string and run it again and it will be false.
By the way, Govinda, you have been very helpful in your posts, but your name still hasn't been changed to meet the Ranch's requirements. Please refer to this page, http://www.javaranch.com/name.jsp and re-register with a new name. Thanks
Bill

[This message has been edited by bill bozeman (edited November 29, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic