For full-fledged work with strings in the examples in this section, you should understand a feature that we will not fully disclose in this course.
Namely, the same string value may exist in the program in several instances. If we calculate the value of
"1" + "2" two times, then we get, of course, two identical values of
"12", but these will be exactly two identical values, and not the same (in the computer’s memory in two different places there will be two identical strings).
And this is important because the operations == and !=, being applied to the lines, check whether the same value is compared. And for two independent lines “12” the result of applying == will be false!
In fact, you probably should not dive into this topic right now, our lessons are structured in such a way that you do not need a clear understanding of this principle for a long time, it is important to know its specific effect.
Namely, how in this case is it necessary to compare strings by value?
To compare str1 and str2, you need to write str1.equals (str2) - this boolean expression verifies that the string str1 and the string str2 have equal values. It can be the same value or two separate equal values.
The bottom line: the lines should not be compared using ==, but using equals.
Line inequality, similarly, should be checked not with !=, But as a negation of equality, using the logical negation operator: !str.equals(str2).
In previous lessons, it is mentioned that extracting a character from a string by its index returns a value of type “character”, not “string”. It is necessary in this connection to keep in mind two facts:
1) comparing a character and a string through equals always gives an inequality,
2) characters can be compared with each other by ==, in this sense, characters are closer to numbers than to lines.
Write a function
isMister, which takes a string and checks whether it is the word
Exercise available only for signed users.
Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.