Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int x = 0;
boolean big = x != 0 && 10 / x > 1;
System.out.println(big);
}
}false
- Expecting an
ArithmeticException:&&evaluates left to right and skips the right side once the left isfalse, so10 / xnever runs. - Expecting a compile error: the compiler does not follow the value of
x; a division by a variable compiles, and only a run could fail.
- What changes if the two sides of
&&are swapped? - When does
||skip its right side?