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
?
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
boolean bool = (aString.indexOf(subString) != -1);
in order to make a strong visual distinction between the two operators using the =
symbol, but this is not required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With