When the JVM loads a class containing a string literal
String str = "hello";
it reads a string literal from a UTF-8 encoded class file and creates a char array from it
char[] a = {'h', 'e', 'l', 'l', 'o'};
then it creates a String object from this char array using the String constructor (char [])
new String(a)
then the JVM places the String object in the String pool and assigns a reference to this String object to the str variable.
source share