How many objects did new String("abc") create?

First of all, there is a new keyword in this code, this keyword is when the program is running, according to the already loaded
The system class String is a string object instantiated in the heap memory.
Then, in the construction method of this String, a "
abc” string, because String inside
The string member variable of is final modified, so it is a string constant.
Next, the JVM takes the literal "
abc” go to the string constant pool to try to get its corresponding String pair
Like a reference, if it is not available, a String object of "abc" will be created in the heap memory
And save the reference to the string constant pool.
Subsequent if there is a literal "
abc", because the literal value already exists in the string constant pool
abc”, so you only need to obtain the corresponding reference from the constant pool, and you don’t need to create it again.
So, for this question, I think the answer is
1. If the string constant abc does not exist, create two objects, namely the string constant abc,
And the instance object new String.
2. If the string constant abc exists, only one object will be created

Guess you like

Origin blog.csdn.net/weixin_55229531/article/details/131488589