• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

String concat

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String x="abc"
System.out.println(x.concat("def")); // output is abcdef


String x = "abc"
s.concat("def") // this object gets created but gets lost/abandoned , why so ?
System.out.println("x=" + x) // output is x=abc


Can someone explain why the above two print statements behave differently.

 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:String x = "abc"
s.concat("def") // this object gets created but gets lost/abandoned , why so ?
System.out.println("x=" + x) // output is x=abc


If you are doing s.concat(), obviously the value of x remains the same, i.e. "abc".
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the s.concat() is a typo, I think the reason for the output is that, after concatenation, you don't assign its value to anything.

jose chiramal wrote:this object gets created but gets lost/abandoned , why so ?


The object gets lost because you don't assign it to anything.
 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable, they never change. All methods that return a String return a different String object. The original String (s / x) stays the same even if you ignore the result of the concat method.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case when I say

String x = "abc"

System.out.println(x.concat("def")) // output is x=abcdef

Does it mean that the System.out.println() by default calls a toString() that returns a new String Object ?


 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
man, String is final.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic