开发者

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(aBoole开发者_如何学Can){
   //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)


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.


if the statement is unreachable this will always give compile time error.....

eg. if you use return statemnt b4 any code this will give compile time error.

but if you use this type of code

if(aBoolean){ //do something }

these will not be avoided by java or not removed byjava........

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜