title: Why did the first for loop fail in Java?
body: <p>I have a int[] a, and trying to set every element in the a to be 1. So when I did following code, and printed every element, it shows they are still 0s.</p>
  
  <pre><code>        for(int num:a) 
                num=1;
  </code></pre>
  
  <p>But if I try below, every element is 1 now. I'm confused. I always thought the 2 for loop have the same functionality. Can anyone tell me why my first try failed? And why it works when i print them? Thanks~~~</p>
  
  <pre><code>        for(int num=0;num&lt;a.length;num++) 
              a[num]=1;
          for(int n:a)
              System.out.println(n);
  </code></pre>