title: How to determine if the byte array is all zero in java
body: <p>I was having some problem when trying to loop thru byte array. Basically I got two nested for loop. The outer one will loop for 256 times, the inner one will loop for the first 8 bytes of byte array.</p>
  
  <pre><code>outerloop:
      for (int i = 0; i &lt; 256; i++){
          // code
  
          byte [] first8bytes = Arrays.copyOfRange(byteArray, 0, 8);
  
          for(int k = 0; k &lt; first8bytes.length; k++){
              if(k != 0){
              }else{
                  System.out.println(Arrays.toString(first8bytes));
                  break outerloop;
              }
          }        
      }
  </code></pre>
  
  <p>What I am trying to do is to check if the first8bytes is all 0. If one of them is not zero, I will go back to the outerloop. However, by doing this, when I try to print out the first8bytes, the result is [-26, 0, -28, -49, 64, 45, 66, 66]. </p>
  
  <p>I am trying to get the first8bytes of all 0 before breaking out. I know something is really wrong with my logic but I have no idea how to fix it.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Updated</p>
  
  <pre><code>boolean all0 = true;
  
      for (int i = 0; i &lt; 256; i++){
      byte [] first8bytes = Arrays.copyOfRange(byteArray, 0, 8);
      for( int k = 0; k &lt; first8bytes.length &amp;&amp; all0; ++k ){
              if( first8bytes[k] != 0 ) all0 = false;
      }
      if(all0){
          System.out.println(Arrays.toString(first8bytes));
          break;
       }}
  </code></pre>