Are the below two pieces of code the same?
String foo = \"foo\";
String foo = new String(\"foo\").intern();
The first one i.e.
String foo = "foo";
in this line, we are creating a String using String literals. That means the string is automatically saved in String Constant pool.
In the 2nd one , i.e. -
String foo = new String("foo").intern();
Here we are creating a String using new String() & then manually saving it to the String constant pool. If we didn't have mentiones intern() , it would not have saved in the String constant pool.
For more clarification, please refer to this link -
http://javacodingtutorial.blogspot.com/2013/12/comparing-string-objects-intern-other.html