My Question is what's the use of creating string object in string pool as well as on Heap when we declare String as String a = new String("abc");
What is the advantage ?
And why not we create string in heap when we create string as String a = "abc"
.
new
does not create a string pool entry. The literal does that before you even callnew
. If you donew String(a)
, nothing is created in the string pool. – ProclusInteger x = 123;
also does not create an object in the heap (it takes it from a pool, same as with Strings). – Proclus