The equals() method compares two Strings: a literal containing a space (as the first and only character) and
- the first character of a String object called str (in the first part of the boolean expression)
- the last character of str (in the second part of the boolean expression)
What does this comparison actually say?
If the first character of str is not a space OR the last character of str is not a space, then... *do something*
Beginning programmer - self-taught
Oracle Certified Associate Java SE 8
Mike Simmons wrote:Also if the substrings are just one character each, it's probably easier to use chars:
Actually, because we have a concise "not equals" operator "!=", but no con concise "not or" operator, I find the first code easier to understand. But either works.
Your two conditions are not the same. The first one, which is the accurate translation, returns true if either the first or the last character is not a space. The second (wrong) one, returns true unless the first or last character is a space. It's equivalent to firstChar != ' ' && lastChar != ' ', which switches the boolean operator.