Jump to content

Does Java "compile out" unnecessary code??

- - - - -

  • Please log in to reply
1 reply to this topic

#1
ReignInChaos

ReignInChaos

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
After having a discussion with a co-worker, we were in disagreement on if the Java compiler "compiles out" unnecessary code. I have heard rumors that C compilers do this during preprocessing, but I have not found any documentation that agrees or disagrees with these statements. If anyone has heard similar statements or better yet has some form of documentation that proves/disproves the above, please reply.

For example, some say that

doSomething();

if ( false )
{
  ...
}

doSomethingElse();

would get compiled into
doSomething();

doSomethingElse();


Any thoughts???

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Class I made:
/**
 * User: wimDC Date: 9/11/11 Time: 15:26
 */
public class Test
{
    public void anything(String unused){
        if(false){
            System.out.println("never printed.");
        }
    }
}
Compiled, and then decompiled it:
/* Decompiled through IntelliJad */
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packfields(3) packimports(3) splitstr(64) radix(10) lradix(10) 
// Source File Name:   Test.java

public class Test
{


    public Test()
    {
    }


    public void anything(String s)
    {
    }
}

Edit: you also can't debug that part, it just skips it.

Edited by wim DC, 10 November 2011 - 07:46 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users