【问题标题】:("kg"=="kg") returns false. How Do I tell java, that this comparison returns true? [duplicate]("kg"=="kg") 返回错误。我如何告诉java,这个比较返回true? [复制]
【发布时间】:2023-03-19 09:39:01
【问题描述】:

可能重复:
Java string comparison?

我试图这样做:

boolean exit = false;
while(exit==false && convStoreIndex<convStoreLength) {
  if(conversionStore[convStoreIndex].getInUnit()==inUnit) {
    indexCount++;
    exit=true;
  }
  convStoreIndex++;
}

但是 if 条件永远不会成立,即使两个字符串是相同的(在调试器中检查了这一点)。 所以我添加了一些行:

boolean exit = false;
while(exit==false && convStoreIndex<convStoreLength) {
  Log.v("conversionStore["+String.valueOf(convStoreIndex)+"]", conversionStore[convStoreIndex].getInUnit()+"|"+inUnit);
  String cs = conversionStore[convStoreIndex].getInUnit();
  String iu = inUnit;
  Log.v("cs", cs);
  Log.v("iu", iu);
  Log.v("Ergebnis(cs==iu)", String.valueOf(cs==iu));
  if(conversionStore[convStoreIndex].getInUnit()==inUnit) {
    indexCount++;
    exit=true;
  }
  convStoreIndex++;
}

这里是 LogCat 的摘录:

09-15 11:07:14.525: VERBOSE/cs(585): kg
09-15 11:07:16.148: VERBOSE/iu(585): kg
09-15 11:07:17.687: VERBOSE/Ergebnis(cs==iu)(585): false

conversionStore 的类:

class ConversionStore {
  private String inUnit;
  [...]
  public String getInUnit() {
    return inUnit;
  }
}

谁疯了,java还是我?

【问题讨论】:

  • 您的标题也具有误导性,因为您没有“kg”=="kg"(我相信这实际上会评估为真)。
  • Und bitte in Zukunft !exit statt exit == false schreiben.

标签: java android


【解决方案1】:

不要使用==比较String对象,使用.equals()

if(conversionStore[convStoreIndex].getInUnit().equals(inUnit)) {

【讨论】:

  • 我真的很高兴我今天已经超过了我的 200 分上限,因为获得 140 代表这个答案是错误的。特别是因为它是一个明显的重复。
【解决方案2】:

要比较字符串是否相等,请不要使用 ==。 == 运算符检查 看看两个对象是否是完全相同的对象。两个字符串可能是 不同的对象,但具有相同的值(具有完全相同的 其中的字符)。使用 .equals() 方法比较字符串 平等。

直接来自 Google 在搜索“Java 字符串比较”时提供的第一个链接...

【讨论】:

    【解决方案3】:

    请使用String.equals()按字符串内容而不是引用身份进行比较。

    【讨论】:

      【解决方案4】:

      您正在使用== 比较您的Stringscs==iu

      但只有当两个Strings 实际上是同一个对象时,这才会返回true。这不是这里的情况:您有两个不同的 String 实例,它们包含相同的值。

      你应该使用String.compareTo(String)

      【讨论】:

        【解决方案5】:

        使用.equals(); 方法插入==

        Becase ::

        它们的意义有很大不同。 equals() 方法存在于 java.lang.Object 类中,它应该检查对象状态的等价性!这意味着,对象的内容。而 '==' 运算符应检查实际对象实例是否相同。

        例如,假设您有两个 String 对象,它们被两个不同的引用变量 s1 和 s2 指向。

         s1 = new String("abc");
         s2 = new String("abc");
        

        现在,如果你使用“equals()”方法来检查它们是否等价

        if(s1.equals(s2))
          System.out.println("s1.equals(s2) is TRUE");
        else
          System.out.println("s1.equals(s2) is FALSE");
        

        当 'equals()' 方法检查内容等效性时,您将得到 TRUE 输出。

        让我们检查一下 '==' 运算符..

        if(s1==s2)
         System.out.printlln("s1==s2 is TRUE");
        

        否则 System.out.println("s1==s2 为假");

        现在您将得到 FALSE 作为输出,因为 s1 和 s2 都指向两个不同的对象,即使它们共享相同的字符串内容。这是因为每次创建新对象时都会使用“new String()”。

        尝试在不使用“new String”的情况下运行程序,而只使用

        String s1 = "abc";
        String s2 = "abc";  
        

        这两个测试你都会得到 TRUE。

        共振::

        After the execution of String str1 = “Hello World!”; the JVM adds the string “Hello World!” to the string pool and on next line of the code, it encounters String str2 =”Hello World!”; in this case the JVM already knows that this string is already there in pool, so it does not create a new string. So both str1 and str2 points to the same string means they have same references. However, str3 is created at runtime so it refers to different string.

        【讨论】:

          【解决方案6】:

          两件事: 你不需要写 "boolean==false" 你可以写 "!boolean" 所以在你的例子中是:

          while(!exit && convStoreIndex<convStoreLength) { 
          

          第二件事: 要比较两个字符串,请使用 String.equals(String x) 方法,这样可以:

          if(conversionStore[convStoreIndex].getInUnit().equals(inUnit)) { 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-08
            • 2020-07-30
            • 2018-02-10
            • 2019-01-22
            • 2014-02-10
            • 2012-04-12
            • 1970-01-01
            相关资源
            最近更新 更多