String str = “This is string literal”;
This is a string literal. When you declare a string like this you are actually calling intern()
method on String
.
This method references an internal pool of string objects. If there already exists a string value “This is string literal”, then str
will reference that string and no new String
object will be created.
String str = new String(“this is string created by new operator”);
This is a string object. The JVM is forced to create a new String
reference, even if "this is string created by new operator" already exists in the reference pool.