在for循环中声明的变量的范围

Mer*_*ary 3 java

for(int i=0; i<10;i++){
 int j=0;
}
Run Code Online (Sandbox Code Playgroud)

是ja块变量还是局部变量?我看到j的范围只有for循环结束

Sur*_*tta 8

局部变量在方法,构造函数或块中声明.

由此可见, 所有块变量都是局部变量.

根据Block的定义

块是平衡括号之间的一组零个或多个语句,可以在允许单个语句的任何位置使用.

所以

{   //block started

}    //block ended
Run Code Online (Sandbox Code Playgroud)

在块内声明的变量是什么,范围仅限于该块.

for(int i=0; i<10;i++){
 int j=0;
}
Run Code Online (Sandbox Code Playgroud)

因此J范围仅限于该块内部.这是循环.

for(int i=0; i<10;i++){
 int j=0;
 //do some thing with j ---> compiler says "yes boss"
}
//do some thing with j ---> compiler says "Sorry boss, what is j ??"
Run Code Online (Sandbox Code Playgroud)