+ 1

"0" != "0" Java (на русском, українській тоже пишите/також пишіть)

I wrote my code in Java, and when testing it, I realized that "0" != "0", here is this part of the program: m = input.next() if (m == 0){ ... This code in 'if' does not work. I checked it out of the program and it really is, why?

14th Jul 2022, 1:15 PM
Сергей Петренко
Сергей Петренко - avatar
3 ответов
+ 7
When using "string1==string2", Java checks if both strings have the same address in memory. Instead you should use "string1.equals(string2)" to check if two strings have the same content This is because String isn't a data type but it's a class containing an array of chars and a punch of methods.
14th Jul 2022, 1:23 PM
Michel Beṭar
Michel Beṭar - avatar
+ 1
Thank you!
21st Jul 2022, 4:20 PM
Сергей Петренко
Сергей Петренко - avatar
0
welcome :D
21st Jul 2022, 4:21 PM
Michel Beṭar
Michel Beṭar - avatar