+ 1
Why this doesn't work?
I started learn new programming Lang , but there are a lot of differences with JavaScript, help pls to fix this code(можно на русском обьяснение) https://code.sololearn.com/cpMMs5lHY0q2/?ref=app Why 0 != 0 Or 1 != 1 (In input print 0101010)
1 ответ
+ 1
Down the line, the courses will tell you that in Java you can't compare String with the == or != operators.
You need to do a.equals("0") or !a.equals("0")
At first, it is easier to just accept this fact.
Here are the detailed reasons: For general sanity, Strings are objects, and in Java the == and != operators applied on objects don't check for equality. They serve to tell if two variables point to the same object (to simplify you could say, if they direct to the place in memory), or if a variable is null.
When you want to check if two distincts, or two potentially distincts, objects are to be considered equals, well you'd need to define what counts as equality for two objects of this class. That's why it is done as calling a method, called equals(), which you can redefine.
In the case of Strings, they're equals when they have the same characters in the same order, so if their content is exactly the same and can't be distinguished.