Search code examples
javastringpool

String Literal Pool


Well, I've got such a code

String s = "hello";
String s2 = s + "world";

I know that variable s is stored in Java Heap in String Literal Pool. Also I know that variable s2 is stored in Java Heap (because '+' in this example creates a new object). But my question is: will "world" be put into Pool or nothing will happen with it?


Solution

  • Yes it will. Wherever / Whenever , you have a String literal declared, it goes to pool obviously, regardless the usage of it.