title: Is modulo slow in Java?
body: <p>I've been looking at the implementation of <code>ThreadLocal</code> in the JDK, out of curiosity, and I found this :</p>
  
  <pre><code>/**
   * Increment i modulo len.
   */
   private static int nextIndex(int i, int len) {
       return ((i + 1 &lt; len) ? i + 1 : 0);
   }
  </code></pre>
  
  <p>It looks fairly obvious that this could be implemented with a simple <code>return (i + 1) % len</code>, but I think these guys know their stuff. Any idea why they did this ? </p>
  
  <p>This code is highly oriented towards performance, with a custom map for holding thread-local mappings, weak references to help the GC being clever and so on, so I guess this is a matter of performance. Is modulo slow in Java ?</p>