Why does this if statement, with an assignment and equality check, evaluate to false?
public static void test() {
boolean test1 = true;
if (test1 = false || test1 == false) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
Why is this printing No
?
Because of operator precedence. It is equivalent to this:
boolean test1 = true;
if (test1 = (false || test1 == false)) {
...
}
The part in brackets evaluates to false
.
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