http://www.ohchr.org。 如果你真的需要做<条码>新指令”(<>abc”)条码>,你就知道你需要并且知道为什么。 说你永远不需要的话,它就很罕见了。 Just use abc”
.
长期版本:
您的代码new String ("abc”)
时,在不同时间发生下列情况:
- When the class containing that code is loaded, if a string with the characters
"abc"
is not already in the intern pool, it s created and put there.
- When the
new String("abc")
code is run:
- A reference to the
"abc"
string from the intern pool is passed into the String
constructor.
- A new
String
object is created and initialized by copying the characters from the String
passed into the constructor.
- The new
String
object is returned to you.
如果在使用新版本创建的“abc”时,也把str放在集合中,那么,为什么intern(<>
>规定,如果“Sting集合”含有另外的扼杀物,则从集合体中提取的str物就在集合中添加。
因为intern
。 请注意,打字字字字面上的intern
是一个绝版;插字面全自动相互连接。 例如:
String s1 = "abc"; // Get a reference to the string defined by the literal
String s2 = s1.intern(); // No-op
System.out.println(s1 == s2); // "true"
System.out.println(s1 == "abc"); // "true", all literals are interned automatically
我也想知道,我们是否利用当时的新东西来创造力量?
您至少创建了一个<代码>String的物体(新的、未加附的物体),可能还有两个物体(如果该字面上已经排在集合体内;但是,在装上字面时,该笔记本再次出现:
String s1 = "abc"; // Get a reference to the string defined by the literal
String s2 = new String(s1); // Create a new `String` object (guaranteed)
System.out.println(s1 == s2); // "false"
String s3 = s2.intern(); // Get the interned version of the string with these characters
System.out.println(s1 == s3); // "true"