Java – the string class constructor called when creating a string object using string literal

See the English answer > How can a string be initialized using ""? 10

Example:

String str = "hello";

In this case, what is the constructor of the string class?

Solution

When the JVM loads a class containing string literals

String str = "hello";

It reads string literals from class files in UTF-8 encoding and creates a char array from them

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

It then uses the string (char []) constructor to create a string object from this char array

new String(a)

The JVM then places the string object in the string pool and assigns a reference to the string object to the str variable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>