Skip to content
May 15, 2011 / oop123

Conditional Compilation in Java

Like Double Braces Initialization, this is another interesting thing on Java I found on the Internet.

For those who don’t know what conditional compilation is, it is when the compiler compiles code differently depending on some external condition. For example, while you’re developing an application, you may want to have logging done for debugging purposes. However, you wouldn’t want those code to be in your finished application because they can increase the program’s size and slows down the program. This is where conditional compilation comes in; during development, code such as logging will be compiled along with the rest of the code, but once you finished the program, you can just tell the compiler to ignore those extraneous codes during compilation.

Conditional compilation in Java can be accomplished by exploiting two tricks (every modern) Java compilers use to optimize codes. The first one is the inlining of static final primitives as an optimization. The Java compiler will replace all references to a static final primitive (i.e. int, boolean, long) with the primitive’s actual value; for example, let’s say you have a static final int RATIO = 2, the compiler will look at your code and replace every instance of RATIO with the number 2. The second one is the elimination of unreachable code, like the following:

//the following code blocks will be removed during compilation
if (false) {
    //stuff happened
}

By using these two optimizations, you can turn code blocks on and off during compilation with a final static boolean flag. For example:

public class Demo {
    public static final boolean IN_DEV = true;

    public static void main(String[] args) {
        if (IN_DEV) {
            //debugging code
        }
        //application code
    }
}

If you changed IN_DEV into false in the source code, the IN_DEV contained in the if code block would be inlined to false, and the if block would be eliminated as dead code. The source code that was actually compiled would look something like this:

public class Demo {
    public static final boolean IN_DEV = false;

    public static void main(String[] args) {
        //application code
    }
}

This is a pretty neat trick and it’s a shame that Beginner Java books and tutorials don’t cover it at all, but at least now you know.

Advertisements

3 Comments

Leave a Comment
  1. samanalvi / May 24 2011 7:26 pm

    Hey! I was actually wondering how you got your code snippets to show up in that neat format style. I was looking around and its achievable by using a certain javascript, which for wordpress requires you to use a plugin (available for free). I couldn’t find where to install the plugin though?

    I was wondering if you could let me know how you managed to do so? I would very much appreciate it, thanks!

    • oop123 / May 25 2011 6:07 am

      (Source: Posting Source Code) Well, for wordpress.com, you can just wrap any code you have with ” and ” like this:

      [sourcecode language="java"]
      //code here

      [/sourcecode]

      For wordpress.com, you can’t install any plugins. Plugins can only be installed using wordpress.org software.

      • samanalvi / May 25 2011 5:58 pm

        That worked perfectly!! Thanks so much =)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: