3 ms·
In Java you usually use `.equals()` to test equality, or if your argument is a boolean value: if (myVar) { // } Instead of `myVar == true/fals
by Freak_NL 9y ago
In Java you usually use `.equals()` to test equality, or if your argument is a boolean value:
if (myVar) {
//
}
Instead of `myVar == true/false`.
The accidental assignment is much less common due to the way equality is tested in Java.
Also, `null` comparisons being assigned will fail to compile (assuming var is a String here):
TestApp.java:6: error: incompatible types: String cannot be converted to boolean
if (var = null) {
- tigershark 9y agoBut in Java it’s much easier to do the error of using == instead of equals if you always jump language.
- Freak_NL 9y agoSure, but that is such a common mistake that all Java IDE's warn you when you try to use == for Strings and normal non-number objects.