Java 中的作用域规则

发布于 2024-10-20 15:54:59 字数 536 浏览 5 评论 0原文

有人可以帮助我理解 Java 中的作用域规则吗?这显然是无效的:

    {
        int i = 0;
        System.out.println(i); // fine, of course
    }
    System.out.println(i); // syntax error

i 是在 {} 内声明的,并且在外部不可用。那么这个呢:

    for (int i = 0; i < 10; i++) {
         System.out.println(i); // fine, of course
    }
    System.out.println(i);  // syntax error, same as above.

我对这里的语法错误感到惊讶。 i 在外部作用域中声明,但稍后不可用。它是否通过 for 循环的某些特殊规则绑定到内部块作用域?还有其他情况可能会发生这种情况吗?

Can someone help me understand the scoping rules in Java? This is clearly not valid:

    {
        int i = 0;
        System.out.println(i); // fine, of course
    }
    System.out.println(i); // syntax error

i is declared within the {}, and it's not available outside. So what about this:

    for (int i = 0; i < 10; i++) {
         System.out.println(i); // fine, of course
    }
    System.out.println(i);  // syntax error, same as above.

I'm surprised at the syntax error here. i is declared in the outer scope yet it is not available later on. Is it bound to the inner block scope by some special rule for for loops? Are there other scenarios where this can happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-10-27 15:54:59

想想 for 循环实际上是这样表示的:

{
  int i = 0;
  while (i < 10) {
    // your code
    i ++
  }
}

think of for loop actually represented like this:

{
  int i = 0;
  while (i < 10) {
    // your code
    i ++
  }
}
烟凡古楼 2024-10-27 15:54:59

它是否通过 for 循环的某些特殊规则绑定到内部块作用域?

是的,情况确实如此。

显然有局部变量声明:

class Some { 
   public void x( int i ) { 
    System.out.println( i ); // valid 
   }
   int j = i; // not valid 
}

另请参阅:

来自语言规范。

Is it bound to the inner block scope by some special rule for for loops?

Yes, this is exactly the case.

There's obviously the local variable declaration:

class Some { 
   public void x( int i ) { 
    System.out.println( i ); // valid 
   }
   int j = i; // not valid 
}

See also:

From the language specification.

∞觅青森が 2024-10-27 15:54:59

这是 Java 中 for 循环的定义行为。

class ForDemo {
     public static void main(String[] args){
          for(int i=1; i<11; i++){
               System.out.println("Count is: " + i);
          }
     }
}

注意代码如何声明
初始化中的变量
表达。该变量的范围
从其声明延伸到
由 for 控制的块的末尾
语句,因此它可以用在
终止和增量表达式
以及。如果控制变量
外部不需要for语句
循环中,最好声明
初始化中的变量
表达。名称 i、j 和 k 是
常用于控制for循环;
声明它们在
初始化表达式限制了它们
使用寿命并减少错误。

来源: http://download.oracle.com/javase/tutorial/java /nutsandbolts/for.html

It's the defined behavior of the for loop in Java.

class ForDemo {
     public static void main(String[] args){
          for(int i=1; i<11; i++){
               System.out.println("Count is: " + i);
          }
     }
}

Notice how the code declares a
variable within the initialization
expression. The scope of this variable
extends from its declaration to the
end of the block governed by the for
statement, so it can be used in the
termination and increment expressions
as well. If the variable that controls
a for statement is not needed outside
of the loop, it's best to declare the
variable in the initialization
expression. The names i, j, and k are
often used to control for loops;
declaring them within the
initialization expression limits their
life span and reduces errors.

Source: http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文