java - Each call to StringBuffer#toString and StrinBuilder#toString returns new instance or instance from string pool? -


my question if using stringbuffer(or stringbuilder) , if call tostring method on instance multiple times. stringbuffer return new instance of string each time or returns string string pool? ( assuming have not made change stringbuffer in between calls)

only string literals placed in string constant pool. e.g. string s = "abc"; in string pool while string s = new string("abc") not be. tostring() method created new string string returned not literal pool.

whenever tostring() method encountered, new string created.

string constant pool objects referred once again if follows.

string s = "abc"; string s1 = "abc"; 

this means both reference variables s , s1 refer same abc literal in constant pool.

you can find useful articles regarding string constant pool here. http://www.thejavageek.com/2013/06/19/the-string-constant-pool/


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -