My question is if am using a StringBuffer(or StringBuilder) and if I call toString method on the instance multiple times. Will StringBuffer return new instance of String each time or it returns String from String pool? ( assuming I have not made any change to StringBuffer in between the calls)
-
possible duplicate of [Does StringBuilder.toString retain the built string?](http://stackoverflow.com/questions/15724895/does-stringbuilder-tostring-retain-the-built-string) – AllTooSir Jul 18 '13 at 10:42
3 Answers
As per docs of toString() of StringBuffer
Converts to a string representing the data in this string buffer. A new String object is allocated and initialized to contain the character sequence currently represented by this string buffer. This String is then returned. Subsequent changes to the string buffer do not affect the contents of the String.
So, A new String object is allocated and initialized.
String objects
allocated via new operator are stored in the heap, and there is no sharing of storage for the same contents, where as String
literals are stored in a common pool.
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
where s1 == s2 == s3 but s4 != s5

- 120,458
- 37
- 198
- 307
Only string literals are placed in String constant pool. e.g. String s = "abc";
will be in string pool while String s = new String("abc")
will not be. toString()
method created a new string so the string returned will not be from literal pool.
Whenever toString()
method is encountered, a new String will be created.
String constant pool objects will be referred once again only if you do as follows.
String s = "abc";
String s1 = "abc";
This means both reference variables s
and s1
will refer to same abc
literal in the constant pool.
You can find a useful articles regarding the string constant pool here. http://www.thejavageek.com/2013/06/19/the-string-constant-pool/

- 13,410
- 5
- 37
- 56
Yes calling toString
method of StringBuffer
and StringBuilder
will create a new string object everytime as these methods use the new
keyword to return a string.
Here is the code for toString from StringBuffer class:
public synchronized String toString() {
return new String(value, 0, count);
}
Here is the toString method from StringBuilder class:
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}

- 67,789
- 12
- 98
- 136
-
The toString methods always return a new String instance, because that's how they are documented in the API docs. How they are actually implemented is pretty irrelevant. – jarnbjo Jul 18 '13 at 10:55
-
@jarnbjo You can believe either the documentation or the code. Code will always give you more insight than the documentation. I find both of them correct in this case. But i like to dig more into the api code im using to know the internals. – Juned Ahsan Jul 18 '13 at 11:00
-
If you want to know what a method in the standard API does, you have to check the API documentation. If you can find additional knowledge in the actual code, that is just an implementation specific detail which is valid for exactly the VM version you are looking at. You have absolutely no guarantee that other VMs (from other vendors or other versions from the same vendor) show the same behaviour. – jarnbjo Jul 18 '13 at 11:04
-
I have given the code from Oracle jdk libs only. "You have absolutely no guarantee that other VMs (from other vendors or other versions from the same vendor) show the same behaviour." This part applies to documentation as well. Anyways no further arguments, you have an opinion and i appreciate it. – Juned Ahsan Jul 18 '13 at 11:06
-
No, that part does *not* apply to the API documentation. All VMs must implement the functionality from the standard API as documented in the documentation from Oracle. – jarnbjo Jul 18 '13 at 11:10