Why cannot a Java foreach loop be assigned a value

Source: Internet
Author: User

Directly on the code

publicclass test4 {    publicstaticvoidmain(String args[]){        int [] a=newint[3];        for(int j:a){            j=55;        }    }}

The code is very easy, the following is the Java compiled byte code:

 Public classTest2 = Public test4(); Code:0: Aload_01: invokespecial#8//Method java/lang/object. " <init> ":() V       4:return   Public Static void Main(java.lang.string[]); Code:0: Iconst_3//Push int type 3 to the top of the stack       1: NewArrayint        //Create an array that specifies the original type. and press its reference value into the top of the stack       3: Astore_1//The top reference value of the stack is stored in the second local variable       4: Aload_1//Push the second reference type local variable to the top of the stack       5: DUP//Copy the stack top value and press the copied value into the top of the stack       6: Astore5       //The top reference value of the stack is stored in the specified local variable, where the array reference is placed in a sixth local variable       8: Arraylength//Get the length value of the array and press into the top of the stack       9: istore4       //The value of the top int of the stack is stored in the specified local variable.        One: Iconst_0//Push int type 0 to the top of the stack       A: Istore_3//Put the value of the top int of the stack into the fourth local variable       -:Goto           -      //Jump to       -: Aload5       //pushes the specified reference type local variable to the top of the stack       -: Iload_3//Push the value stack top of the fourth int type local variable       +: Iaload//pushes the value of the specified index of the int type array to the top of the stack       -: istore_2//Put the value of the top int into the third local variable       +: Bipush -      //Push the single-byte constant Value ( -127~128) to the top of the stack       at: istore_2//Put the value of the top int into the third local variable       -: Iinc3,1        //Add the specified int type variable to the specified value       -: Iload_3//Push the fourth int type local variable to the top of the stack       -: iload4       //pushes the specified int type local variable to the top of the stack       -: IF_ICMPLT -      //Comparison of the size of the stack top two int type, when the result is small at 0 o'clock Jump       -:return            //returns void from the current method}

We just need to focus on the 6,16~23 line. It is possible to find that the code is to take the value out of the array reference copy and place it on top of the stack, and then remove the variable from the top of the stack in local variable 3. Then put 55 on the top of the stack, and then put 55 out of the local variable 3. The operation from the beginning to the end is in local local variables, and does not have any effect on the original value.

Why cannot a Java foreach loop be assigned a value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.