0

String defining difference

Why this code gives me false?: String name1 = "Naveh"; String name2 = new String("Naveh"); Return(name1==name2);

21st Feb 2019, 9:17 PM
Naveh
Naveh - avatar
5 Antworten
+ 1
You need to use equals to compare strings. Return (name1.equals(name2)); let me know how it goes
21st Feb 2019, 9:36 PM
Alandec
+ 1
This has been asked before, over in this thread a few people discussed the details: https://www.sololearn.com/discuss/1624545/?ref=app
21st Feb 2019, 11:50 PM
Schindlabua
Schindlabua - avatar
0
It worked thank you. In general, what is the difference betwwen the two initializations?
21st Feb 2019, 9:49 PM
Naveh
Naveh - avatar
0
The == compares if the objects are identical, equals() compares if the characters of a string match
21st Feb 2019, 10:04 PM
Alandec
0
It is very useful to validate passwords too,you can check by turning a letter to uppercase,the result will be false since characters won't match
21st Feb 2019, 10:19 PM
Alandec