有人可以解释为什么这个 returns 是 "else" 值而不是 "then" 值吗?

can someone explain why this returns the "else" value and not the "then" value?

java 子序列显然为真,但只有 returns 为假值。为什么? 试图查看一个序列是否等于更大字符串的子序列

package testifthen;


public class TestIfThen {

    public static void main(String[] args) {
String result = "01900287491234567489";
String result1 = "90028749";


if (result.subSequence(2, 10) == result1) {
        System.out.println("excel");
    }else {
        System.out.println("not found");

}
  }}

在 Java 中,在检查语义相等性时,.equals 方法应优先于 == 运算符。当您检查两个值 "mean" 是否相同时,应使用 .equals,而 == 检查它们是否是完全相同的对象。

如果没有更多信息很难说(例如这是什么语言)。

假设这是 Java,我会说你的问题是使用带字符串的 == 而不是 .equals 函数。 == 不检查字符串的内容,仅当它们引用相同的对象时。 .equals 应该改用,因为它实际上检查两个字符串中的字符是否匹配

尝试使用

if (result.subSequence(2, 10).equals(result1)) {
    System.out.println("excel");
} else {
    System.out.println("not found");
}

== 符号可能是导致它 return 错误的原因,因为引用不同。

这个 post 应该更多地解释 ==equals() 之间的区别:What is the difference between == vs equals() in Java?