Java内部函数

Java内部函数,java,string,Java,String,我对实习生的职能有点困惑。我有以下代码: 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); //

我对实习生的职能有点困惑。我有以下代码:

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保留字或关键字提供false,因为当我尝试使用en-um时,它提供true,而使用by-te时,它提供false。有人能告诉我为什么吗?

你得到了你所做的输出,因为
java
main
字符串已经在池中了-当你启动应用程序时,有很多其他类被加载,其中一些类已经插入了这些字符串(在你的代码到达之前)

我也希望
native
,但我想不是,这意味着没有其他人实习过

这已经引起了一些注意(我没有预料到),所以我想我会对此进行一点扩展。假设您这样做:

  String left = new String(new char[] { 'h', 'e', 'y' });
  String right = left.intern();
  System.out.println(left == right);
这将打印
true
,因为“hey”以前不在字符串池中,而是由我们的
left.intern()添加的。另一方面:

 String left = new String(new char[] { 'j', 'a', 'v', 'a' });
 String right = left.intern();
 System.out.println(left == right);

打印
false
,因为调用
left.intern时,“java”已经在池中

让我们看看
intern()
的文档:

如果池中已经包含一个由equals(object)方法确定的与此字符串对象相等的字符串,则返回池中的字符串。否则,此字符串对象将添加到池中,并返回对此字符串对象的引用

这意味着,如果池中已有一个具有给定值的字符串,则返回该旧字符串,否则返回实际字符串。 由于前三个字符串不在池中,实际字符串-
s2
s11
s4
在添加到池中后返回,因此它是相同的引用。不知何故,“main”和“java”已经在池中,并且
intern()
正在返回该字符串,而不是
s33
s331
(假设上次调用为
s331.intern()

试试这个:

String tmp = "ANEKSINGH";  // so this is in the pool
String s11 = "ANEK".concat("SINGH");
String s22 = s11.intern();
System.out.println(s11 == s22); // FALSE now
intern()返回字符串对象的规范表示形式。A. 字符串池最初为空,由类私下维护 绳子

调用intern方法时,如果池已包含 字符串等于由equals(对象)确定的此字符串对象 方法,则返回池中的字符串。否则,这个 字符串对象被添加到池中,并引用此字符串 对象返回

返回true的输出表示字符串存在于字符串池中

如果您使用intern()插入新创建的和特定的字符串,则输出将为true

示例:

public class InternTest{

public static void main(String args[]){
    String s1 = "ANEK";
    String s2 =  (new String("ANEK")).intern();
    String s3 = s2.intern();
    System.out.println(s3 == s1); // True

    String s33 = ("ma".concat("in")).intern();
    String s44 = s33.intern();
    System.out.println(s33 == s44); // True

    String s331 = ("ja".concat("va")).intern();
    String s441 = s33.intern();
    System.out.println(s331 == s441); // True
}
}

@YCF_L intern函数给了我字符串的规范表示形式。为什么它是重复的?这不是关于字符串比较的问题,而是关于
intern
usagesting Interning是一种只存储每个不同字符串值的一个副本的方法,它必须是不可变的。在Java中,String类有一个公共方法intern(),该方法返回String对象的规范表示形式。Java的String类私下维护一个字符串池,字符串文本在其中被自动插入。当您比较字符串时,应该使用equals not==它检查字符串常量池并检查引用,然后与字符串进行比较。。我不是在比较两个不同的字符串。。我需要查一下参考资料。因为为s33和s44创建了新的参考。。但它不会为s4和s5创建参考。您的最后第二个语句有拼写错误。它应该是
s331.intern()
而不是
s33.intern()
我发现有趣的是,我在包含
main
方法的类中定义的任何方法的名称(我没有尝试其他类的方法)也已在字符串池中。@Eran使
intern
更像是一种仅用于调试的方法。。。我无法想象有哪一个地方我会明确地想要使用它。。顺便说一句,因为类的解析方式最有可能发生