- Jun 27, 2002
- 2,908
- 0
- 76
Short-circuit evaluation in Java is imperfect. The code below breaks if I remove the method declarations or if I change the left-to-right order of the conditionals.
This leads me to two higher questions about programming in general.
1. Do all interpreters or JIT compilers do a full syntax check before looking for this sort of optimization? (Is syntax-checking always a separate process before run-time optimization?)
2. Is short-circuit evaluation always performed left-to-right? Do any modern compilers ignore critical errors if the code can correctly continue without them?
Code:
public class pears {
public static void main(String[] args) {
if (!(0==1 && unicorn()==pixiedust())) {
System.out.println("The short-circuit evaluation worked!");
}
if (1==1 || unicorn()==pixiedust()) {
System.out.println("The short-circuit evaluation worked!");
}
}
public static int unicorn() {
return 1/0;
}
public static int pixiedust() {
return -1/0;
}
}
This leads me to two higher questions about programming in general.
1. Do all interpreters or JIT compilers do a full syntax check before looking for this sort of optimization? (Is syntax-checking always a separate process before run-time optimization?)
2. Is short-circuit evaluation always performed left-to-right? Do any modern compilers ignore critical errors if the code can correctly continue without them?