Optimizations performed by javac
I've been a full-time Java developer for about a year now, and in that time I've seen a lot of incorrect information out there about the behavior of the Java compiler (javac). With this post I hope to clear up some misconceptions about compile-time optimization in Java. Note that this information is correct for my version of javac but is not necessarily true for all JDKs.
brian@mint ~ $ java -version java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
Boolean expressions
Let's start with an easy case. Is the compiler smart enough to spot a redundant logical NOT in a boolean condition? Let's compile the following blocks of code:
if(!booleanExpression) { doSomething(1); } else { doSomething(0); }
if(booleanExpression) { doSomething(0); } else { doSomething(1); }
The compiler produces pretty much identical bytecode for both of these. Of course, the cost of an added logical NOT operation would be insignificantly small. But in fact there is no extra operation. Let's examine the bytecode:
Read full article from Optimizations performed by javac
No comments:
Post a Comment