Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does new String() update the string pool in Java? [duplicate]

Tags:

java

string

If I write something like this:

String s1 = new String("ABC");
String s2 = "ABC";

In which scenarios does the string pool get updated? The first, second or both?

like image 775
Deepak Sharma Avatar asked Mar 06 '26 06:03

Deepak Sharma


1 Answers

In the above programming code ,when string pool get updated?

The first statement will update/add to the String pool with "ABC" String literal, but create another object in the heap and the variable s1 will refer the heap object.

The second statement will refer the already created Pool String object.

like image 179
Abimaran Kugathasan Avatar answered Mar 08 '26 18:03

Abimaran Kugathasan



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!