关于String,语言规范里有6点总结: This example illustrates six points:Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1). Literal strings within different classes in the same package represent references to the same String object. Literal strings within different classes in different packages likewise represent references to the same String object. Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. Strings computed at run time are newly created and therefore distinct. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. 建议:这方面的问题最好是去看书,经常出现乱七八糟的解释,还是自己好好研究一下比较好。
基本用不到这东西,感觉lz还是不要深究了。
This example illustrates six points:Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. 建议:这方面的问题最好是去看书,经常出现乱七八糟的解释,还是自己好好研究一下比较好。
一个初始时为空的字符串池,它由类 String 私有地维护。 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。 它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。 返回:
一个字符串,内容与此字符串相同,但它保证来自字符串池中。