+ 6

Can someone tell me why these two strings are not equal?

I got a question similar to this right on a challenge, but only because false has one letter more than true. Initializing identical strings with the new keyword or with a String literal returns false when you compare s1==s2. Both strings have the same length attribute and appear identical when printed. What makes them not equal? https://code.sololearn.com/cRD5j1mwKxYj/?ref=app

2nd Sep 2019, 8:22 PM
David Crowley
David Crowley - avatar
19 Respostas
+ 11
Strings go inside somthing called the "String pool" if you have 2 strings created which are the same, java will store the refrenece to an existing one in that variable. String str1 = "hello"; String str2 = "hello"; str1==str2 //true both share the same refrenece Is the same as String str1 = "hello"; String str2 = str1; str1==str2 //true str2 refers to the same refrenece as str1. If you wanted str1 and str2 to refer to separate instances then you need to use the "new" keyword. String str1 = new String( "hello"); String str2 = new String(str1); str1==str2 // false with objects use == to compare references and str.equals() to compare there values
2nd Sep 2019, 9:40 PM
D_Stark
D_Stark - avatar
+ 5
Anton Böhler thanks for the .equals method! I will certainly remember it now!
2nd Sep 2019, 9:42 PM
David Crowley
David Crowley - avatar
+ 3
Strings aren't compared with '==' because the variable holding the String actually stores a pointer to the String. so with '==' you compare the pointers! to check for equality of two String use String.equals(String str) example: String s1 = "no"; String s2 = "no"; if(s1 != s2){ System.out.println("those are two diffrent Objects!"); } if(s1.equals(s2)){ System.out.println("the two Strings have the same value!"); }
2nd Sep 2019, 9:22 PM
Anton Böhler
Anton Böhler - avatar
+ 3
zemiak thanks. That definitely shows what's under the covers. So why do s2 and s3 (the strings declared literally) have the same pointer?
2nd Sep 2019, 9:40 PM
David Crowley
David Crowley - avatar
+ 3
D_Stark you answered my follow-up to zemiak perfectly. Thanks.
2nd Sep 2019, 9:44 PM
David Crowley
David Crowley - avatar
+ 3
devanille I also integrated those code snippets back into my original code to check things out. Thanks for the credit in the comments. Writing code is definitely the best teacher! Cheers.
2nd Sep 2019, 9:46 PM
David Crowley
David Crowley - avatar
+ 3
David Crowley Thanks buddy 😊, also good thing to know if your learning, is that objects types like String are treated differently then primitive types like int,double, char ect.. for example int x = 10; int y = 20; As these are not objects you can compare using x==y because primitive type dont have refrenece like objects do, they store the actual value so behind the scenes it looks like 10==20 👍
2nd Sep 2019, 10:01 PM
D_Stark
D_Stark - avatar
+ 2
try add this System.out.printf( "s1 %s\ns2 %s\ns3 %s\n", System.identityHashCode(s1), System.identityHashCode(s2), System.identityHashCode(s3) );
2nd Sep 2019, 9:20 PM
zemiak
0
Готово
4th Sep 2019, 5:12 PM
Роман Асанов
Роман Асанов - avatar
0
Игры и приложения
4th Sep 2019, 5:12 PM
Роман Асанов
Роман Асанов - avatar
0
Программирование и разработка Ричи 3 100% и правильно ноутбук Асус Асанов Роман Русланович https://code.sololearn.com/Wagf0c2pd311/?ref=app
4th Sep 2019, 5:26 PM
Роман Асанов
Роман Асанов - avatar
0
hi
4th Sep 2019, 5:31 PM
vaculty
vaculty - avatar
0
Ок готово
4th Sep 2019, 5:34 PM
Роман Асанов
Роман Асанов - avatar
4th Sep 2019, 5:38 PM
Роман Асанов
Роман Асанов - avatar
0
Перезагрузка и пременено всё 100% и правильно автоматически
4th Sep 2019, 5:39 PM
Роман Асанов
Роман Асанов - avatar
0
Po
4th Sep 2019, 6:08 PM
Blerim Beqa
Blerim Beqa - avatar
0
Nulopal sk
4th Sep 2019, 6:08 PM
Blerim Beqa
Blerim Beqa - avatar