+ 13

Why the responses are different?

String str1 = new String("some"); String str2 = "some"; System.out.println(str1 == str2); System.out.println(str1.equals(str2));

28th Feb 2017, 10:34 PM
Ghena Ramascan
Ghena Ramascan - avatar
2 Respostas
+ 12
because of the object instance. In the str1, we instantiate a new object of type String. The reference of second one, reference to another instance. So, in the first sout, we compare the instance obj, and get false since they are different. In the second sout, we get true since we compare the values of these two objects. Take care and think twice when you have to write comparison ;)
28th Feb 2017, 10:44 PM
Ghena Ramascan
Ghena Ramascan - avatar
+ 2
For strings alway use .equals method is recomend on Android docs
1st Mar 2017, 1:22 AM
nextco
nextco - avatar