当前位置:网站首页>String s = new string ("XYZ") how many string objects are created?

String s = new string ("XYZ") how many string objects are created?

2022-06-24 00:01:00 The fifth brother of the Wang family

One or two . If the string constant pool already has “xyz”, It is a ; otherwise , Two .

When the character creation constant pool does not “xyz”, The following two objects will be created :

One is the literal amount of a string "xyz" The corresponding 、 Resident (intern) Instances in a globally shared string constant pool , At this time, the instance is also in the heap , String constant pool only puts references .

The other is through new String() Create and initialize , Content and "xyz" Same instance , It's also in the heap .

String s = "xyz" and String s = new String("xyz") difference ?

Both statements will first go to the string constant pool to check whether it already exists “xyz”, If so, use , If not, the... Is created in the constant pool “xyz” object .

in addition ,String s = new String("xyz") Will pass new String() Create a content and in the heap "xyz" Same object instance .

So the former is actually understood as being included by the latter .

 

原网站

版权声明
本文为[The fifth brother of the Wang family]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232121148933.html