When I write boolean bool = aString.indexOf(subString) != -1 Eclipse did not complain, does it mean that it is the same as boolean bool = aString.indexOf(subString) != -1 ? true : false?
When I write boolean bool = aString.indexOf(subString) != -1 Eclipse did not complain, does
Share
Yes. A comparison produces a boolean value, and it can be assigned to a variable just as any other value.
The second form (with the ternary
?:operator) is redundant and should not be used.Stylistically, I normally enclose boolean expressions in parentheses when assigning them to values, as
in order to make a strong visual distinction between the two operators using the
=symbol, but this is not required.