Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why java revert logical operators while compile

Tags:

java

Actual java code is:

((rrd == null || !rrd) 
    && null != dam
    && null != dam.getac()
    && null != dam.getac().getc() 
    && null != sname 
    && sname.equalsIgnoreCase(dam.getac().getc()))

But when I look into class file it's:

((rrd != null) && (rrd.booleanValue())) 
    || ((((null == dam) 
    || (null == dam.getac()) 
    || (null == dam.getac().getc()) 
    || (null == sname) 
    || (!(sname.equalsIgnoreCase(dam.getac().getc()))))))

All || and && interchanged.

Can anyone explain why?

like image 554
Amit Goel Avatar asked Mar 08 '16 12:03

Amit Goel


1 Answers

The expressions are not equivalent but inverted. It looks like the compiler avoids an outer (or implied) not here.

Note that short-circuiting is possible for both operations, || and && -- in the first case when a true sub-expression is encountered and in the second case when a false sub-expression is encountered. So the ability to short-circuit alone does not explain this.

like image 169
Stefan Haustein Avatar answered Oct 31 '22 06:10

Stefan Haustein