Essay

&& vs & in Java

Note to myself:

In java, conditional && will not evaluate the right-hand operand if the left-hand operand is false.

If you want to check the right-hand operand use &. For example:

public class MainClass {
public static void main(String[] arg) {
int value = 8;
int count = 10;
int limit = 11;
if (++value % 2 == 0 & ++count < limit) {
System.out.println("here");
System.out.println(value);
System.out.println(count);
}
System.out.println("there");
System.out.println(value);
System.out.println(count);
2 collapsed lines
}
}
Terminal window
Output:
there
9
11