Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Android code optimization: Does Java remove unreachable code on build

Please take a look at this code android sample :

private static final boolean aBoolean = false;

...

if(aBoolean){
   //do something like logs
}

In this case since the value of aBoolean is false and that it can't change at runtime, would the //do something like logs statement be ignored by on build or will it be still be build and each time it will evaluate the if?

I'm trying to find a behavior like pre processor #DEFINE #IF... so that when I'm coding I get my logs, when I release I switch one value and all my debug code gets completely ignored.

(also I would like to point out that my question is android oriented so if there is a difference between Java and Android on this matter please let me know)

like image 903
Jason Rogers Avatar asked Jan 31 '11 05:01

Jason Rogers


1 Answers

Checking a variable for logging is perfectly find. Even if the code doesn't get optimised out, checking a boolean condition is almost a no-op and you are very prematurely optimising.

But to answer your question, it probably gets optimised out.

like image 76
Falmarri Avatar answered Oct 21 '22 13:10

Falmarri