+ 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
19 odpowiedzi
+ 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
+ 5
Anton Böhler thanks for the .equals method! I will certainly remember it now!
+ 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!");
}
+ 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?
+ 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.
+ 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 👍
+ 2
try add this
System.out.printf( "s1 %s\ns2 %s\ns3 %s\n",
System.identityHashCode(s1),
System.identityHashCode(s2),
System.identityHashCode(s3) );
0
Готово
0
Игры и приложения
0
Программирование и разработка Ричи 3 100% и правильно ноутбук Асус Асанов Роман Русланович
https://code.sololearn.com/Wagf0c2pd311/?ref=app
0
hi
0
Ок готово
0
Память Телефона и ноутбука увеличена и установлена сразу программа Антивирус Антихакер 100% и правильно автоматически
https://code.sololearn.com/WW9y1U8mZz1D/?ref=app
https://code.sololearn.com/Wa2fz16ntDjr/?ref=app
https://code.sololearn.com/WNJcYiNyANi1/?ref=app
https://code.sololearn.com/WrHF05Da5p4v/?ref=app
https://www.sololearn.com/discuss/1953992/?ref=app
https://www.sololearn.com/discuss/1954651/?ref=app
https://www.sololearn.com/discuss/1954741/?ref=app
https://www.sololearn.com/discuss/1954226/?ref=app
0
Перезагрузка и пременено всё 100% и правильно автоматически
0
Po
0
Nulopal sk