Java – count of string objects created

I'm reading the certificate and I'm having a confusing problem here The book says that this line of code creates only one string object, but I think it creates two objects Am I right?

String summer = new String("Summer");

Isn't the constant text "summer" created and placed in the string constant pool?

Editor: guys, I'm confused. I need the exact answer There are different posts saying that 1objects & 2objects are being created

Solution

Creativity may be the cause of chaos In fact, you're right: there are two string instances in this line

String summer = new String("Summer");

Since two string instances are involved, they must be created at some time

Creation time is the biggest difference "Summer" is a constant, which will be loaded into the internal string pool when loading the class containing this constant Therefore, this internal string instance is created when the class is loaded

The string object referenced by the variable summer is created when the code containing this line is run nothing more.

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
分享
二维码
< <上一篇
下一篇>>