Java If(false)编译 [英] Java If(false) Compile

查看:227
本文介绍了Java If(false)编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat开发动态Web项目。拥有一个全局标志是很有用的,这是我在开发和部署服务器之间唯一需要更改的标志。该标志的最大用途是使用print语句。

I am developing a dynamic web project using Tomcat. It is useful to have a global flag that is the only thing I have to change between my development and deployment servers. The biggest use of the flag is with print statements.

public class Debug {
    public final static boolean DEVEL = true;        
    public static void print(String message){
        if(DEVEL){
            System.out.println(message);
        }
    }
}

我的问题是,请问java编译出print语句。即如果devel标记为false,则消息显然不会打印,但它们将包含在类文件中(devel是final)。这是一个效率问题。我听说java编译器很聪明,但它会接受这个。

My question is, will java compile out the print statements. i.e. if the devel tag is false, the messages will obviously not print but will they be included in the class files (devel is final). This is a question of efficiency. I've heard that the java compiler is very smart but will it pick up on this.

推荐答案

使用'final'属性(正如你所做的那样),它将编译出代码:

use the 'final' attribute (as you did), and it will compile out the code:

public static final boolean DEVEL = false;

您可以通过在结果类文件中查找仅在代码编译时出现的字符串来检查。

You can check by grepping the resultant class file for a string that would appear only if the code were compiled.

这篇关于Java If(false)编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆