property
The value is used for character storage.
Implementation Note: This field is trusted by the VM, and is a subject to constant folding if String instance is constant. Overwriting this field after construction will cause problems. Additionally, it is marked with Stable to trust the contents of the array. No other facility in JDK provides this functionality (yet). Stable is safe here, because value is never null.
@Stable
private final byte[] value;method
intern
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s. intern() == t. intern() is true if and only if s. equals(t) is true.
All literal strings and string-valued constant expressions are interned. String literals are defined in section @jls 3.10.5 of the The Java Language Specification.
Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
返回字符串对象的规范表示。
最初为空的字符串池由String类String 。
当调用intern方法时,如果池已经包含与equals(Object)方法确定的相当于此String对象的字符串,则返回来自池的字符串。 否则,此String对象将添加到池中,并返回对此String对象的引用。
由此可见,对于任何两个字符串s和t , s.intern() == t.intern()是true当且仅当s.equals(t)是true 。
所有文字字符串和字符串值常量表达式都被实体化。 字符串文字在The Java™ Language Specification的 3.10.5节中定义。
结果: 一个字符串与该字符串具有相同的内容,但保证来自一个唯一的字符串池。
查询字符串常量池中是否具有该字符串,如果有,返回该字符串的索引,如果没有,在字符串常量池中创建该字符串然后返回索引。
public native String intern();exercise
String a = "hsp"; // a 指向 常量池的 “hsp”
String b = new String("hsp");// b 指向堆中对象
System.out.println(a.equals(b)); // True, ok
System.out.println(a == b); // False
// b.intern() 方法返回常量池地址
System.out.println(a == b.intern()); // True
System.out.println(b == b.intern()); // False根据 intern()方法的文档内容:
if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned.
因为String a = "hsp"; a 指向了 常量池的 “hsp”,也就是说常量池存在了"hsp" 所以 b.intern() 返回的是 常量池里的 "hsp"的引用。因此System.out.println(a == b.intern()); 为 true。
System.out.println(b == b.intern()); 这条语句 b 指向堆中的 value 对象,b.intern() 还是返回常量池里的 "hsp"的引用,所以这两个不是同一个对象。
String s1 = "hspedu"; //指向常量池”hspedu”
String s2 = "java"; //指向常量池”java”
String s4 = "java";//指向常量池”java”
String s3 = new String("java");//指向堆中对象
System.out.println(s2 == s3); // False
System.out.println(s2 == s4); // True
System.out.println(s2.equals(s3));// True
System.out.println(s1 == s2); // FalsePerson p1 = new Person();
p1.name = "hspedu";
Person p2 = new Person();
p2.name = "hspedu";
System.out.println(p1.name.equals(p2.name));//比较内容: True
System.out.println(p1.name == p2.name);
// True,不是 new 的 在常量池里,所以p1和p2指向同一个内容
System.out.println(p1.name == "hspedu"); //True,同样都指向常量池
String s1 = new String("bcde");
String s2 = new String("bcde");
System.out.println(s1==s2); // False,都是 new 出来的,所以为不同的对象