Hacker News new | ask | show | jobs
by Freak_NL 2966 days 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) {
1 comments

But in Java it’s much easier to do the error of using == instead of equals if you always jump language.
Sure, 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.