What is the difference between a String object and a String literal in Java?

Java 8Object Oriented ProgrammingProgramming

When the String literal used to create String, JVM initially checks weather String with the same value in the String constant pool, if available it creates another reference to it else it creates a new object and stores it in the String constant pool.

In case of an object, each time you instantiate the class a new object is created given value irrespective of the contents of the String constant pool.

Updated on 30-Jul-2019 22:30:20