我对实习生功能有点困惑。我有以下代码:Java实习生功能
public class InternTest{
public static void main(String args[]){
String s1 = "ANEK";
String s2 = new String("ANEK");
String s3 = s2.intern();
System.out.println(s3 == s1); // True
String s11 = "ANEK".concat("SINGH");
String s22 = s11.intern();
System.out.println(s11 == s22); // True
String s4 = "nat".concat("ive");
String s5 = s4.intern();
System.out.println(s4 == s5); // True
String s33 = "ma".concat("in");
String s44 = s33.intern();
System.out.println(s33 == s44); // false
String s331 = "ja".concat("va");
String s441 = s331.intern();
System.out.println(s331 == s441); // false
}
}
我的问题是关于输出。在第三种情况下,它给了我的真实性,但在第四和第五种情况下,它给了我虚假。我能否知道这些输出背后的原因是什么?我不能得出结论,它为java保留字或关键词提供了假,因为当我尝试使用en时,它给出了真实性,但是通过te它给了我假。任何人都可以告诉我为什么?
@YCF_L实习生功能给我的规范表示字符串.. –
为什么它是重复的?这不是关于字符串比较的问题,而是关于'intern'的使用 – ByeBye
字符串实习是一种仅存储每个不同字符串值的一个副本的方法,它必须是不可变的。 在Java中,String类具有公共方法intern(),它返回字符串对象的规范表示形式。 Java的String类私有维护一个字符串池,其中字符串文字被自动实现。而当你比较字符串,你应该使用等于不是== –