Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does StringBuilder.append() method fill up the string pool?

I know that using most of String's methods will return a new string object which is added to the string pool and that the string literals passed as arguments to these methods also are added to the string pool.

For example the code below will create three string objects namely: "Hello ", ""World!"" and "Hello World!" which are added to the string pool implying that the argument passed to concat() method is added to the string pool.

What I am curious to know whether the string literal arguments passed to StringBuilder.append() method and any other methods in StringBuilder are added to the string pool.

Or maybe I should ask whether all string literal arguments that are passed to any other method in any other class also are added to the string pool ?

I also want to add the I know these facts based on java 6.

String s = "Hello " ;
s = s.concat("World!") ;
like image 371
user9349193413 Avatar asked Aug 08 '26 13:08

user9349193413


1 Answers

What I am curious to know whether the string literal arguments passed to StringBuilder.append() method and any other methods in StringBuilder are added to the string pool.

Or maybe I should ask whether all string literal arguments that are passed to any other method in any other class also are added to the string pool ?

All String literals are put into the string pool. How you use the literal is irrelevant.

like image 58
Code-Apprentice Avatar answered Aug 11 '26 03:08

Code-Apprentice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!