• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Strings

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at the following code....
public class TrimTest{
public static void main(String args[]) { String blank = " "; // one space
String line = blank + "hello" + blank + blank;
line.concat("world");
String newLine = line.trim(); System.out.println((int)(line.length() + newLine.length())); }}
When run the output is 13.I am unable to understand this question. Can sombedoy please clafiry.
TIA
Tanuja
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you will edit your code and put in code tags before and after your code, I would be happy to look at it.
But the way it's currently written, it's hard to see where the blocks are, and if it's hard, chances are most people won't take the time to look at it.
This link explains how to use UBB tags.
Thanks.
 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here we go......
public class TrimTest
{
public static void main(String args[])
{
String blank = " "; // one space
String line = blank + "hello" + blank + blank;
line.concat("world");
String newLine = line.trim();
System.out.println((int)(line.length() + newLine.length()));
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 13 because Strings are immutable.
line.concat("world") returns a new string object " hello world" but you are not storing the object refernce anywhere.
newline = "hello"\\ 5 chars after trim.
line = " hello "\8 chars
So the final result is 13.
 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable hence line.concat("world")will not return a new String object ,although
line=line.concat("world")will return a new String object.Am i right............
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tanuja, your second post was better, but you can make your text much cleaner by putting in
[ code ] (no spaces) before the code, and
[ /code ] (no spaces) after the code.
You can edit your existing messages. Click the edit button at the top of your post.
 
Rasri Anand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line.concat("world") will also return a new object. But that object 's refernce is not being stored anywhere.
When you do line = line.concat("world")
line is now pointing to the new object. So you are able to access it.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanuja Vaid:
Strings are immutable hence line.concat("world")will not return a new String object ,although
line=line.concat("world")will return a new String object.Am i right............

Not quite. line.concat( "world" ) does return a new String object (ignoring any String pool efficiency considerations), but it is not used (a reference to it is not created and maintained). In your second consideration, a reference to the new String that is returned is indeed maintained.
[ May 15, 2002: Message edited by: Dirk Schreckmann ]
 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob I will definitely remeber that for future.But for now can u please answer the question.........
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no better time than now...
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code to see what is happening and see my explanation below. Hope this helps!

The call to line.concat("world") returns a String object which isn't caught and the contents of line doesn't change. So, after line.concat ("world"), line still has the contents " hello ". That's a white space, the word "hello" and two more white spaces for a total of 8 characters. So line has length 8 and newLine removes the white space so it has length 8 - 3 white space characters or 5. 8 + 5 = 13. The bottom line here is that the concat() method returns a new String object...
 
reply
    Bookmark Topic Watch Topic
  • New Topic