String declared using new and = have differences.
Difference in String Declaration |
String Literals (Declared using = sign)
When a String is declared using = sign it is considered as String literal.
When a String is declared using = sign it is stored in String Constant Pool rather than making a new object for memory management.
String Constant Pool looks something like this
String Constant Pool |
If the String is already there in the Pool the object will point at the location of the String which is already in the pool rather than making it a new entry.
Consider a project made for a company with name Asterix Solution , imagine how many times this single String will be used. Image at the end will clear the doubt
String Object(Declared using new operator)
When a String is declared using new keyword, it is considered as a normal object and unlike literals they go in Object Heap as a normal object rather than going in String Constant Pool.