Which way of appending to a string is more optimal? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Which way of appending to a string is more optimal?

I heard this statement from competitive programmer, that is, Appending to a string as str += str2 is far more optimal than str = str + str2, because the latter creates a new string and then replaces its version stored in str. Is this true? If yes, then why? Please give the explanation in simple terms.

11th Sep 2019, 9:23 AM
Ashish
Ashish - avatar
18 Answers
+ 5
I think this is true.
11th Sep 2019, 9:31 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
~ swim ~ and yeah thanks for clearing things out and giving me time. You seem to be a guy always out there to help someone and not someone who is not very welcoming.
11th Sep 2019, 2:44 PM
Ashish
Ashish - avatar
+ 3
From what I understand you'll just end up creating multiple objects when adding strings together and should instead use something like StringBuilder 🤷‍♂️
11th Sep 2019, 7:12 PM
will
will - avatar
+ 3
str+=str2 and str=str+str2 is equivalent, so there is no difference between this two variants String is immutable in java and in any way you are creating new strings If you want to build some String from many strings then it is better to use StringBuffer or StringBuilder
11th Sep 2019, 10:24 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 2
~ swim ~ Yo! I get it now, thanks :)
11th Sep 2019, 10:12 AM
Ashish
Ashish - avatar
+ 2
Oh! You could be right
11th Sep 2019, 2:16 PM
Ashish
Ashish - avatar
+ 2
~ swim ~ btw is it only me or you also feel facing some difficulty when different websites use different words(technical terms) but they mean the same. And also while reading official docs.
11th Sep 2019, 2:18 PM
Ashish
Ashish - avatar
+ 2
Hmm. But still do you struggle while reading docs thats been regarded as difficult to perceive?
11th Sep 2019, 2:30 PM
Ashish
Ashish - avatar
+ 2
Oh! Boosted my confidence! Was feeling so low lately after reading some docs of certain topic.
11th Sep 2019, 2:37 PM
Ashish
Ashish - avatar
11th Sep 2019, 9:32 AM
Ashish
Ashish - avatar
+ 1
For me this is nothing but a syntactic sugar for the latter.
11th Sep 2019, 9:33 AM
Ashish
Ashish - avatar
+ 1
~ swim ~ Bro, i didn't get whats inplace in this context. Can you give me an example?
11th Sep 2019, 9:41 AM
Ashish
Ashish - avatar
+ 1
~ swim ~ over somewhere, i guess.
11th Sep 2019, 9:47 AM
Ashish
Ashish - avatar
+ 1
~ swim ~ maybe thats why string references are immutable in java?
11th Sep 2019, 10:20 AM
Ashish
Ashish - avatar
+ 1
So in short, when we use former way, there are two variables playing, and in the latter, there are actually three variables doing the thing, i.e, str, str2, temporary(that gets stored in string pool), am i right?
11th Sep 2019, 10:35 AM
Ashish
Ashish - avatar
+ 1
Didn't I mention Java? Oh! I am so sorry
11th Sep 2019, 11:16 AM
Ashish
Ashish - avatar
+ 1
~ swim ~ oh! My bad. Btw isnt a string reference a concept in c++ as well?
11th Sep 2019, 11:23 AM
Ashish
Ashish - avatar
0
append() of class String is more optimal only if the variables aren't initialized. Otherwise, str+= str2 is better if the variables are already initialized
12th Sep 2019, 6:23 PM
Sourav Singh
Sourav Singh - avatar