title: Compiling without assertions
body: <p>I know assertions can be enabled/disabled at runtime for debugging and production, respectively. However I found that assertions also increase the size of the generated binary (about 100-200 bytes in the example below).</p>
  
  <p>In C and C++, we can do this at compile time by having <code>#define NDEBUG</code> before <code>#include &lt;assert.h&gt;</code>.</p>
  
  <p>Is there any way for the Java compiler to automatically do this? I'd like to leave them in the source code for debugging purposes later on. But I also don't want the resultant binary to be any larger than necessary (we have a size limit as design requirement).</p>
  
  <p>C code:</p>
  
  <pre><code>//#define NDEBUG
  #include &lt;assert.h&gt;
  
  int main(void) {
      assert(0); // +200 bytes without NDEBUG, 0 with NDEBUG
      return 0;
  }
  </code></pre>
  
  <p>Java code:</p>
  
  <pre><code>public class Test {
      public static void main(String[] args) {
          assert(System.nanoTime()==0); // increases binary size by about 200 bytes
      }
  }
  </code></pre>
  
  <hr>
  
  <p>In response to bn.'s answer:</p>
  
  <pre><code>public class Test2 {
      public static final boolean assertions = false;
  
      public static void main(String[] args) {
          if(assertions) {
              assert(System.nanoTime()==0);
          }
      }
  }
  </code></pre>
  
  <p>EDIT: In fact, it seems to me that this enabling/disabling is a more useful compile-time feature than run-time. I mean, how many end users will enable them? As far as a programmer is concerned during the debug process, he/she will likely be recompiling code anyways.</p>