Create new lines in regular memory and in the row pool
The first syntax (String str1 = "abc";)
creates only one String object and specifies one reference variable. The object is created in a pool of String constants maintained by the JVM. In the second case String str2 = new String("def");
, two String objects are created. Since new is called, one String object is created in normal memory. In addition, the string constant "newstring" will be put into the String constant pool.
So when we don't have a new keyword, we only have one String object in the pool of String constants.
source to share
In java, whichever string is created using the new keyword will be created in heap memory. If you create any String without using a new one, it will be created in the String pool and named String Constant. There will only be one copy of the String pool value, which means there won't be duplicates in the string pool.
source to share