I'm testing with this code:
public class TestNull {
public void leftComparison(String s) {
if (s == null);
}
public void rightComparison(String s) {
if (null == s);
}
}
I compiled it with javac 1.8.0_05, and then inspected the bytecode:
public class TestNull {
....
public void leftComparison(java.lang.String);
Code:
0: aload_1
1: ifnonnull 4
4: return
public void rightComparison(java.lang.String);
Code:
0: aconst_null
1: aload_1
2: if_acmpne 5
5: return
}
Apparently, leftComparison is compiled to push and pop 1 variable on the stack while rightComparison pushes and pops 2. I speculate that leftComparison is therefore slightly more efficient than rightComparison?
I'm wondering why the compiler doesn't rewrite the code of rightComparison? In my opinion, the two comparisons should be semantically equivalent, right?
ifnotsbytecode. javac is very literal in how it translates things.