title: Does order of conditions matter with multiple conditions in a while loop?
body: <p>I was not able to find the answer to this question. I was working on an insertion sort method and it wouldn't properly execute:</p>
  
  <pre><code>public static &lt;T extends Comparable&lt;? super T&gt;&gt; void insertionSort(T[] array) {
      int length = array.length;
      T temp;
      for (int i = 1; i &lt; length; i++) { //start of unsorted
          temp = array[i]; //save the element
          int j = i-1;
          while (temp.compareTo(array[j]) &lt; 0 &amp;&amp; j &gt;= 0) { // while temp is less than array[j]
              array[j+1] = array[j]; 
              j--;
          } //end of while
          array[j+1] = temp; //as soon as temp is greater than array[j], set array[j] equal to temp
      }
  }
  </code></pre>
  
  <p>This returned an <code>ArrayIndexOutOfBoundsException</code> on the while loop line, but when I switched the conditions in the while loop around to this:</p>
  
  <pre><code>while (j &gt;= 0 &amp;&amp; temp.compareTo(array[j]) &lt; 0)
  </code></pre>
  
  <p>it worked. I didn't think in Java the order of conditions in a while loop mattered to the program? This is very strange to me, as I've never seen or heard of order mattering in a statement with <code>&amp;&amp;</code> since I assumed that the two while loop lines were equivalent.  I was stuck wondering this for a while and couldn't find an answer. </p>
  
  <p>Can someone explain why this is so?</p>