title: Java assertions - $assertionsDisabled vs $assertionsEnabled
body: <p>Assertions in java compile down to a private synthetic static boolean added to a test - the proposal is nicely documented here:</p>
  
  <p><a href="http://jcp.org/aboutJava/communityprocess/jsr/asrt_prop.html" rel="noreferrer">JSR Assertion Proposal</a></p>
  
  <p>In it, we create </p>
  
  <p>final private static boolean <strong>$assertionsEnabled</strong> =
          ClassLoader.desiredAssertionStatus(className);</p>
  
  <p>and then <pre>assert(X)</pre> becomes <pre>if ($assertionsEnabled &amp;&amp; !x) { throw }</pre></p>
  
  <p>Which makes perfect sense ;)</p>
  
  <p>However, I've noticed that what I actually get is</p>
  
  <pre><code>public void test1(String s) {
      assert (!s.equals("Fred"));
      System.out.println(s);
  }
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>static final /* synthetic */ boolean $assertionsDisabled;
  
  public void test1(String s) {
      if ((!(AssertTest.$assertionsDisabled)) &amp;&amp; (s.equals("Fred"))) {
          throw new AssertionError();
      }
      System.out.println(s);
  }
  
  static {
      AssertTest.$assertionsDisabled = !(AssertTest.class.desiredAssertionStatus());
  }
  </code></pre>
  
  <p>I can't find any documentation as to why they went with a NEGATIVE test, rather than a positive test - i.e. the original proposal captured assertionsENABLED, now we use assertionsDISABLED.</p>
  
  <p>The only thing I can think of is that this would possibly (POSSIBLY!) generate better branch prediction, but that seems like a pretty lame guess to me - the Java philosophy is (almost) always to make the bytecode simple, and let the JIT sort out optimisations. </p>
  
  <p>( note that this isn't a question about how assertions work - I know that! :) )</p>
  
  <p>( As an aside, it's quite interesting to see that this leads to incorrect tutorials! 6.2.1 <a href="http://etutorials.org/Programming/Java+performance+tuning/Chapter+6.+Exceptions+Assertions+Casts+and+Variables/6.2+Assertions/" rel="noreferrer">of this tutorial</a>, which someone quoted in response to <a href="https://stackoverflow.com/questions/10716970/anyone-else-knows-what-does-this-mean">a previous SO question on assertions</a> gets the sense of the test wrong! :)</p>
  
  <p>Any ideas?</p>