==
, for this purpose.
It's not uncommon for even experienced programmers to write ==
when they mean =
or vice versa. In fact this is a very common cause of errors in C programs. Fortunately in Java, you are not allowed to use ==
and =
in the same places. Therefore the compiler can catch your mistake and make you fix it before you can run the program.
However there is one way you can still get into trouble:
boolean b = true;
if (b = false) {
System.out.println("b is false");
}
To avoid this, some programmers get in the habit of writing
condition tests like this:
boolean b = true;
if (false = b) {
System.out.println("b is false");
}
Since you can't assign to a literal, this causes a compiler
error if you misuse the =
sign when you mean to write ==
.