• Post Reply Bookmark Topic Watch Topic
  • New Topic

concatenate and join difference  RSS feed

 
Ashwini Sidhanti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I wrote a small piece of code thinking that join and concatenate works in a similar way. But end up not figuring out why it doesn't work with concatenate. Here is the code.


Output :
Join String: a,b,c,d
name is: a
name is: b
name is: c
name is: d
Concatenate String:

Please guide me through if there is some code changes required.
Regards
Ashwini Sidhanti
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Since a String is immutable, you are losing the results of the concat call; you shou‍ld assign it back to concatString:-
concatString = concatString.concat(name).concat(",");
That will of course produce a different result. I shall leave you to work out how different. It is also inefficient in terms of performance to use concat or similar inside a loop.
 
Ashwini Sidhanti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie. An important feature of string which I obviously forgot.         Thanks once again 
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!