• 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

Question about Strings?

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class string
{
public static void main(String[] args) {
String str1="str";
str1.concat("str1"); //Line 0
System.out.println(str1); // Line 1
System.out.println(str1.concat("str1"));//Line 2
}
}
Output -
str
strstr1
Please explain me the output. I know that Strings are immutable and will create a new String at Line0 but wouldn't the same thing happen at Line2.
Will be really obliged if somebody could explain it.
Thanks in Advance,
Ira
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ira Jain,
String str1="str";
str1.concat("str1"); //Line 0
System.out.println(str1); // Line 1
System.out.println(str1.concat("str1"));//Line 2
In Line 0 an anonymous string is created ("strstr1") but str1 is not changed.
That is why when you print in Line 1 str is getting printed.
With that reason again in Line 2 the anonymous string strstr1 is printed.
Hope this helps.
rajani

[This message has been edited by rajani peddi (edited December 21, 2000).]
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True a new string is created, but you didn't assign that new String to anything, so str1 reamained unchanged. If you said:
str1 = str1.concat("str1");
You would get what you expected when you printed out str1.
Bill
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi,
String str1="str";
this line creates a string object "str" in string pool and its reference is in str1.
str1.concat("str1");
here u create an anonymous new string in a pool "strstr1"
but u did'nt print that.
If u use System.out.println here u will see tha same result at line 2.
System.out.println(str1);
this line is ok, it prints str.

System.out.println(str1.concat("str1"))
now u use this, therefore it prints strstr1.
so at line 0, u create a string but not print it.
at line 2 u again do the same thing but print it.
hope it helps.
 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys !! You really cleared up the concept for me.
reply
    Bookmark Topic Watch Topic
  • New Topic