A small question about the for loop

Source: Internet
Author: User

There are the following procedures:

 Package com.lk.b;  Public class TEST5 {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub         for (int i=0;i<10;i++)             New Integer (i);            System.out.println ("Hello World");}    }

There may be a lot of people think this is right, in fact you are wrong, this program is not compiled by the compiler, because the integer k = new integer (i) this sentence. The For loop may not use {}, but is limited to executing statements (where the variable life statement is not included), since the scope of the integer k in this code is scoped to the entire main method, resulting in a duplicate definition of the variable error. Therefore, errors occur at compile time. To correct this, just add a pair of curly braces and let the variable be declared inside the block. As follows:

1  Packagecom.lk.b;2 3  Public classTEST5 {4 5      Public Static voidMain (string[] args) {6         //TODO auto-generated Method Stub7          for(inti=0;i<10;i++){8Integer k =NewInteger (i);9         }TenSystem.out.println ("Hello World"); One     } A  -}

A small question about the for loop

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.