Java自动实习字符串文字。这意味着在许多情况下,==运算符似乎适用于
Strings,其处理方式与
ints或其他原始值的用法相同。
由于
Interning对于
String文字是自动的,因此该
intern()方法将用于使用
new String()
使用你的示例:
String s1 = "Rakesh";String s2 = "Rakesh";String s3 = "Rakesh".intern();String s4 = new String("Rakesh");String s5 = new String("Rakesh").intern();if ( s1 == s2 ){ System.out.println("s1 and s2 are same"); // 1.}if ( s1 == s3 ){ System.out.println("s1 and s3 are same" ); // 2.}if ( s1 == s4 ){ System.out.println("s1 and s4 are same" ); // 3.}if ( s1 == s5 ){ System.out.println("s1 and s5 are same" ); // 4.}将返回:
s1 and s2 are sames1 and s3 are sames1 and s5 are same



