title: Fastest way to iterate over all the chars in a String
body: <p>In Java, what would the fastest way to iterate over all the chars in a String, this:</p>
  
  <pre><code>String str = "a really, really long string";
  for (int i = 0, n = str.length(); i &lt; n; i++) {
      char c = str.charAt(i);
  }
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>char[] chars = str.toCharArray();
  for (int i = 0, n = chars.length; i &lt; n; i++) {
      char c = chars[i];
  }
  </code></pre>
  
  <p><strong>EDIT :</strong></p>
  
  <p>What I'd like to know is if the cost of repeatedly calling the <code>charAt</code> method during a long iteration ends up being either less than or greater than the cost of performing a single call to <code>toCharArray</code> at the beginning and then directly accessing the array during the iteration.</p>
  
  <p>It'd be great if someone could provide a robust benchmark for different string lengths, having in mind JIT warm-up time, JVM start-up time, etc. and not just the difference between two calls to <code>System.currentTimeMillis()</code>.</p>