0

I have a problem with Integer.toString conversion. This code outputs "ololo". Why? And how can I convert integer to string right?

 String str1= "1";
 String str2=Integer.toString(1);
 if (str1!=str2)Log.d("myLogs","ololo");    
4

4 回答 4

2

You must compare Strings using equals method, not == nor != operators since they will compare the String object references.

if (!str1.equals(str2)) {
    Log.d("myLogs","ololo");
}

Note that when you use Integer#toString you're creating a new String that is not in the String JVM pool, thus getting the error described.

于 2013-04-21T14:51:40.787 回答
0

字符串比较必须用equals来完成。
if (!str1.equals(str2))...

当你使用!=你得到引用相等(不等式)

于 2013-04-21T14:52:11.213 回答
0

尝试String.valueOf(1);将整数更改为字符串。

于 2013-04-21T14:52:51.690 回答
0

改为使用!str1.equals(str2)

你不应该使用==or!=字符串

于 2013-04-21T14:52:59.640 回答