Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: What will the compiler remove from the code?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default What will the compiler remove from the code?

    Hi there.

    Edit: I googled some more and found out, that the java compiler does almost no optimization at all.
    Java relies on the JIT to do all kinds of optimization.
    But there are 3rd party applications to optimize the byte code generated by a compiler a little more.

    I have a quick question: How good is the compiler at noticing dead code and removing it before creating byte-code?
    Lets assume I have the following code:
    public class ImportantClass {
     
    	private static final boolean DEBUG = false;
     
    	public void importantTask() {
    		doStuff();
    		debug("This is an important message!");
    	}
     
    	private void debug(String text) {
    		if (DEBUG) {
    			System.out.println(text);
    		}
    	}
     
    	private void doStuff() {
    		// This is very complicated stuff right here!
    	}
     
    }
    Would the compiler remove the call to "debug"?
    Would the compiler remove the method "debug(String)"?
    If it would not, is there some simple way for me to implement a conditional output which is removed from the final release?

    I am working with Eclipse Juno SR2 at the moment if this is important.

    Thank you very much.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What will the compiler remove from the code?

    Unreachable statements cause a compiler error.

    Empty blocks, like a while( true ) block with nothing in it, are removed per the compiler's optimization logic.

    In your example above, I don't believe the call to debug() would be removed, because debug() contains logic that would have to be evaluated, but I can't point to a reference that confirms that, and I could be wrong.

    And, No, the IDE you're using is not important or even relevant.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: What will the compiler remove from the code?

    Quote Originally Posted by GregBrannon View Post
    Unreachable statements cause a compiler error.

    Empty blocks, like a while( true ) block with nothing in it, are removed per the compiler's optimization logic.

    In your example above, I don't believe the call to debug() would be removed, because debug() contains logic that would have to be evaluated, but I can't point to a reference that confirms that, and I could be wrong.
    I was just thinking that, since DEBUG is a final variable and set to false, the compile might realize that the entire if-block can be removed.
    And then, since the method "debug(...)" doesnt contain anything anymore (after the if-block has been removed) the entire method might get removed.
    And if the method is removed then obviously the call to the method would need to be removed as well.

    I know that some fancy C compilers can do stuff like this.

    Quote Originally Posted by GregBrannon View Post
    And, No, the IDE you're using is not important or even relevant.
    If I am not mistaken there are different Java-compilers out there. So perhaps a different IDE might use a different compiler. I dont really know, but thanks for clarifying.

    Edit: After googling a bit about it I read on wikipedia, that eclipse has its own java compiler. Whether the eclipse version I use actually uses this different java compiler is not known to me.
    Source: http://en.wikipedia.org/wiki/Java_compiler

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What will the compiler remove from the code?

    When in doubt, run your code through a dissassembler. The Oracle JDK includes a javap tool which allows you to decompile the output.

    I copied your code into test.java (default compiler options, Oracle JDK 1.7.0_40), compiled it, and ran javap -p -c test

    Here's the output:
    Compiled from "test.java"
    public class test {
      private static final boolean DEBUG;
     
      public test();
        Code:
           0: aload_0       
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return        
     
      public void importantTask();
        Code:
           0: aload_0       
           1: invokespecial #2                  // Method doStuff:()V
           4: aload_0       
           5: ldc           #3                  // String This is an important message!
           7: invokespecial #4                  // Method debug:(Ljava/lang/String;)V
          10: return        
     
      private void debug(java.lang.String);
        Code:
           0: return        
     
      private void doStuff();
        Code:
           0: return        
    }

    So Oracle's javac compiler did remove the dead code inside of debug(), but it did not remove the call to debug(). This is in general the case with Java because of the modular nature of Java that removing the call to debug() can't be done at compile time reliably.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: What will the compiler remove from the code?

    Okay, thank you very much.
    I have, however, found several programs which optimize the byte-code after compilation and claim to fix problems like these.

Similar Threads

  1. compiler
    By jamarrufo in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 11th, 2013, 09:16 PM
  2. compiler
    By olfat in forum The Cafe
    Replies: 10
    Last Post: March 8th, 2013, 01:06 AM
  3. compiler
    By olfat in forum The Cafe
    Replies: 1
    Last Post: February 22nd, 2013, 03:18 PM
  4. compiler
    By olfat in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 22nd, 2013, 02:25 PM
  5. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM