HOWTO · Java

The Difference Between `!=` and `=!` in Java

Learn how Java's `!=` comparison differs from assigning a negated boolean with `a = !b`.

On this page

In Java, != is the not-equal comparison operator: it evaluates to true when its operands have different values. The character sequence =! is not an operator. In an expression such as a = !b, Java parses = as assignment and ! as boolean negation, so the negated value of b is assigned to a.

The examples below show the boolean result of each comparison and how assignment combined with negation affects a condition.

Java != Not-Equal Comparison Example

Use != to compare two values. It returns false when they are equal and true when they differ. For primitive numeric values, it compares the values; for reference values, it compares whether the references identify different objects. To negate an equality comparison, write !(a == b), which has the same boolean result as a != b.

The operator produces a boolean, so it fits directly in an if condition or can be stored in a boolean variable. For example, if (count != limit) runs when the two primitive values differ. When comparing objects, != checks identity rather than the contents of the objects; two distinct String objects can contain the same text and still compare as not equal by identity. Use equals when the task is to compare object contents. Parentheses in !(a == b) make the order clear: first evaluate equality, then negate its result. This equivalence is useful when reading a longer condition, though a != b is usually the shorter form.

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = 10;
    System.out.println(a != b);

    a = 20;
    System.out.println(a != b);
    System.out.println(!(a == b));
  }
}
false
true
true

With the first pair of values the comparison is false. After a changes, the values differ, so both the not-equal comparison and the negated equality expression are true.

Java =! Assignment and Negation Example

There is no combined =! operator in Java. In a = !b, Java evaluates !b first and assigns that boolean result to a. The same assignment can appear in an if condition: the condition receives the assigned boolean value, and the block runs when that value is true. This form is legal, but it changes a while checking the condition, so a direct condition such as if (!b) is often easier to read when the assignment is unnecessary.

Assignment has lower precedence than unary negation, so the expression groups as a = (!b). The assignment expression itself also has a boolean value: it evaluates to the value just stored in a. That is why Java accepts it as the condition of if; the condition does not compare a with another value. This can be useful when the assignment is intentional, but it can surprise someone who expects a comparison. If both a saved value and a separate test are needed, assign first and test in a separate statement to make the two actions obvious.

public class Main {
  public static void main(String[] args) {
    boolean a;
    boolean b = true;
    a = !b;
    System.out.println("a = " + a);

    b = false;
    if (a = !b) {
      System.out.println("condition is true; a = " + a);
    }
  }
}
a = false
condition is true; a = true

When b is true, !b is false and that value is assigned to a. Later, when b is false, the assignment in the condition stores true in a; that true value makes the if block run. Spacing the expression as a = !b makes the assignment and negation easier to distinguish, but whitespace does not change its meaning.