java - which String class constructor get called when String object created by using String literal -


this question has answer here:

which constructor of string class called when create string object using string literal .

example:

string str = "hello"; 

in case constructor of string class get?

when jvm loads class containing string literal

string str = "hello"; 

it reads string literal class file in utf-8 encoding , creates char array it

char[] = {'h', 'e', 'l', 'l', 'o'}; 

then creates string object char array using string(char[]) constructor

new string(a) 

then jvm places string object in string pool , assigns reference string object str variable.


Comments