简体   繁体   English

Java 7-String.intern()行为

[英]Java 7 - String.intern() behaviour

I've read this answer about how to check if a string is interned in Java, but I don't understand the following results: 我已经阅读了有关如何检查Java中是否存在字符串的答案 ,但我不理解以下结果:

String x = args[0]; // args[0] = "abc";
String a = "a";
String y = a + "bc";
System.out.println(y.intern() == y); // true

But if I declare a string literal: 但是,如果我声明一个字符串文字:

String x = "abc";
String a = "a";
String y = a + "bc";
System.out.println(y.intern() == y); // false

Besides, without any string literal, the args[0] seems to be directly interned: 此外,在没有任何字符串文字的情况下, args[0]似乎可以直接插入:

// String x = "abc";
String y = args[0];
System.out.println(y.intern() == y); // true (???)
// false if the first line is uncommented

Why does y.intern() == y change depending on whether x is a literal or not, even for the example when the command-line argument is used? 为什么y.intern() == y会根据x是否为文字而变化,即使对于使用命令行参数的示例也是如此?

I know literal strings are interned at compile time , but I don't get why it affects in the previous examples. 我知道文字字符串在编译时会被嵌入 ,但是我不明白为什么它会影响前面的示例。 I have also read several questions about string interning, like String Pool behavior , Questions about Java's String pool and Java String pool - When does the pool change? 我还阅读了一些有关字符串实习的问题,例如字符串池行为有关Java的字符串池Java字符串池的问题-池何时更改? . However, none of them gives a possible explanation to this behaviour. 但是,他们都没有对此行为提供可能的解释。

Edit: 编辑:

I wrongly wrote that in third example the result doesn't change if String x = "abc"; 我错误地写道,在第三个示例中,如果String x = "abc";结果不会改变String x = "abc"; is declared, but it does. 被声明,但确实如此。

It is because y.intern() gives back y if the string was not interned before. 这是因为如果之前未对该字符串进行过y.intern() ,则y.intern()会返回y If the string already existed, the call will give back the already existing instance which is most likely different from y . 如果字符串已经存在,则调用将返回最可能与y不同的已经存在的实例。

However, all this is highly implementation dependent so may be different on different versions of the JVM and the compiler. 但是,所有这些都高度依赖于实现,因此在不同版本的JVM和编译器上可能有所不同。

Implementation details might differ. 实施细节可能有所不同。 But this is exactly the behavior I would expect. 但这正是我所期望的行为。 Your first case means that commandline arguments are not interned by default. 您的第一种情况意味着默认情况下不会保留命令行参数。 Hence y.intern() returns the reference to y after interning it. 因此, y.intern()在对y进行引用之后将其返回。

The second case is where the VM automatically interns the literal, so that y.intern() returns the reference to x , which is different from y . 第二种情况是VM自动实习文本,因此y.intern()返回对x的引用,该引用不同于y

And the last case again happens because nothing is interned by default, so the call to intern() returns the reference to y . 最后一种情况再次发生,因为默认情况下没有任何干预,因此对intern()的调用将返回对y的引用。 I believe it is legal to intern String more aggressively, but this is the minimal behavior required by the spec as I understand it. 我认为更积极地实习String是合法的,但是据我所知,这是规范要求的最小行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM