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");
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");
You must compare String
s 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.
String comparison must be done with equals.
if (!str1.equals(str2))...
When you use !=
you get reference equality (inequality)
use !str1.equals(str2)
instead.
You shouldn't use ==
or !=
for String