Java 子字符串方法-相等性测试失败

Java 子字符串方法-相等性测试失败,java,substring,equality,Java,Substring,Equality,以前有人问过,但没有真正回答 如果substring方法创建了一个新对象,并且该对象在池中被“interned”,那么为什么==返回false。这是我的密码: public class StringTest { public static void main(String[] args) { String str1 = "The dog was happy"; String str2 = "The hog was happy"; Syste

以前有人问过,但没有真正回答

如果substring方法创建了一个新对象,并且该对象在池中被“interned”,那么为什么
==
返回false。这是我的密码:

public class StringTest {
    public static void main(String[] args) {
        String str1 = "The dog was happy";
        String str2 = "The hog was happy";
        System.out.println(str1.substring(5));// just to make sure
        System.out.println(str2.substring(5));

        if (str1.substring(5) == str2.substring(5)) {
            System.out.println("Equal using equality operator");
        } else {
            System.out.println("Not equal using equality operator");
        }

        if (str1.substring(5).equals(str2.substring(5))) {
            System.out.println("Equal using equals method");
        } else {
            System.out.println("Not equal using equals method");
        }

    }
}
输出为:1。奥格很高兴。奥格很开心。使用相等运算符4不相等。使用相等方法相等

注意:这是子字符串方法源代码:

public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
}
公共字符串子字符串(int-beginIndex,int-endIndex){
如果(beginIndex<0){
抛出新的StringIndexOutOfBoundsException(beginIndex);
}
如果(结束索引>计数){
抛出新的StringIndexOutOfBoundsException(endIndex);
}
如果(beginIndex>endIndex){
抛出新的StringIndexOutOfBoundsException(endIndex-beginIndex);
}
返回((beginIndex==0)和(&(endIndex==count))?这是:
新字符串(偏移量+开始索引,结束索引-开始索引,值);
}

另外,这个问题已经被标记为由几个人提问和回答,但我在Stack Overflow上找不到一个实例,其中有人真正解释了为什么子字符串(0)将计算为true。

答案在于您的问题本身

尝试执行以下操作:

String str1 = "The dog was happy";
 String str2 = "The dog was happy";
 if (str1 == str2) 
 {
    System.out.println("Equal using equality operator");
 } 
  else 
 {
      System.out.println("Not equal using equality operator");
 }
您将得到“使用相等运算符相等”。原因是您已经将str1和str2都声明为字符串文字,并且在这里应用了interned pool的概念

现在,当您按照字符串类定义执行str1.substring(5)时,它将返回一个对象,而不是一个文本。因此==给出错误的结果

字符串类子字符串方法:

return(beginIndex==0)?这是:新字符串(值、beginIndex、SubCN)

如果希望==在子字符串上工作,请在子字符串上使用intern()方法

更改:
if(str1.子字符串(5)=str2.子字符串(5))

if(str1.substring(5.intern()==str2.substring(5.intern())


一个很好的文档可以理解字符串创建和池的详细信息:

您没有
intern
子字符串
str1.substring(5.intern()==str2.substring(5.intern()
@Apollonius。不,这是一个被“拘留”在池中的对象。。。你认为这会自动发生吗?为什么?谢谢你的回复。我的困惑源于这里的问题陈述()及其答案。正如我所说,我必须回答一个非常类似的考试问题(我是一名学生),我不完全确定我是否理解它。我知道substring()方法将创建两个不同的对象,因此==操作将失败,但我不理解为什么在上述问题中,语句1是真的,而语句2不是真的。很明显,我遗漏了一些东西。我终于找到了这种奇怪行为的答案,我在这里发布给其他可能有同样问题的人。substring()方法(在Java7中更改)将始终返回一个新对象。唯一的例外是返回整个字符串(子字符串(0))。然后,它将从池中返回它,等式表达式(=)将计算为true。我理解。子字符串创建两个不同的对象,因此相等运算符失败。非常感谢。