(“kg”==“kg”)返回false。我如何告诉java,这个比较返回true?

问题描述:

可能重复:
Java string comparison?(“kg”==“kg”)返回false。我如何告诉java,这个比较返回true?

我试图做到这一点:

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

但如果条件从来没有真正的,即使两个丝线的相同(在调试器中检查过)。 所以我加了一些行:

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还是我?

+3

你的标题也是误导,因为你没有 “公斤” == “公斤”(我认为,实际上评估为true)。 –

+1

在Zukunft'!exit' statt'exit == false' schreiben中bit bit。 – starblue

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

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

我其实很高兴今天我已经超过了我的200分上限,因为得到140代表这个答案就是错的。特别是因为它是一个明确的重复。 –

请使用String.equals()通过字符串的内容,而不是参考身份比较。

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

直接从谷歌搜索 “Java字符串比较” 时提供的第一个链接...

您使用==是在比较你的Stringscs==iu

但是,只有当两个Strings实际上是相同的对象时,才会返回true。情况并非如此:您有两个不同的String实例包含相同的值。您可以使用String.compareTo(String)

两件事情: 你不需要写“布尔==假”,你可以只写那么在你的榜样,这将是“布尔!”:

while(!exit && convStoreIndex<convStoreLength) { 

第二件事: 比较两个字符串使用字符串。等于(字符串x)的方法,所以这将是:

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

使用==

becase的的.equals();方法instread ::

他们都在他们的意义不同非常多。 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"); 

您将得到输出TRUE作为“的equals()”的方法检查内容平等。

让检查 '==' 操作者..

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

别的 的System.out.println( “S1 == s2为FALSE”);

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

尝试不“新字符串”,只是用

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

您将获得真正的两个测试运行程序。

立信::

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.