简体   繁体   English

为什么我在这里需要括号? Java:“if(true)int i = 0;”

[英]Why do I need brackets here? Java: “if (true) int i=0;”

public class Test{
   public void newMethod(){

      if(true)int i=0;

   }
}

The above code gives me the following error 上面的代码给出了以下错误

Test.java:4: error: '.class' expected
      if(true)int i=0;
                  ^

But if I write it like this 但如果我这样写

public class Test{
   public void newMethod(){

      if(true){
         int i=0;
      }

   }
}

then there is no error! 那就没有错误!

I know this question isn't helpful to the community, but I'm really curious why I need to have brackets in this statement. 我知道这个问题对社区没有帮助,但我真的很好奇为什么我需要在这个声明中有括号。 I've been programming in java for a few years and I've only encountered this error just now. 我已经用java编程了几年,我刚刚遇到这个错误。

I'm using JGrasp by the way. 顺便说一句,我正在使用JGrasp。

Here is my understanding. 这是我的理解。 Quoting from Chapter 14 of JAVA SE 7 specification : 引自JAVA SE 7规范的第14章

14.2. 14.2。 Blocks

A block is a sequence of statements, local class declarations, and local variable declaration statements within braces. 块是大括号内的语句,本地类声明和局部变量声明语句的序列。

 Block: { BlockStatementsopt } ........ BlockStatement: LocalVariableDeclarationStatement ClassDeclaration Statement 

So a block is always in braces { ... } . 所以一个块总是在大括号{ ... }

14.4. 14.4。 Local Variable Declaration Statements 地方变量声明声明

A local variable declaration statement declares one or more local variable names. 局部变量声明语句声明一个或多个局部变量名称。

 LocalVariableDeclarationStatement: LocalVariableDeclaration ; LocalVariableDeclaration: VariableModifiersopt Type VariableDeclarators ....... VariableDeclarator: VariableDeclaratorId VariableDeclaratorId = VariableInitializer 

....... .......

Every local variable declaration statement is immediately contained by a block. 每个局部变量声明语句都立即由一个块包含。 Local variable declaration statements may be intermixed freely with other kinds of statements in the block. 局部变量声明语句可以与块中的其他类型的语句自由混合。

Now, what does it mean, "immediately contained"? 现在,它是什么意思,“立即包含”?

Some statements contain other statements as part of their structure; 有些陈述包含其他陈述作为其结构的一部分; such other statements are substatements of the statement. 这样的其他陈述是陈述的替代。 We say that statement S immediately contains statement U if there is no statement T different from S and U such that S contains T and T contains U. In the same manner, some statements contain expressions (§15) as part of their structure. 我们说语句S立即包含语句U,如果没有与S和U不同的语句T使得S包含T而T包含U.同样,一些语句包含表达式(§15)作为其结构的一部分。

Let's look at your example: 我们来看看你的例子:

public class Test{
   public void newMethod(){

      if(true)int i=0;

   }
}

In this case we have the following block: 在这种情况下,我们有以下块:

{

          if(true)int i=0;

}

Inside this block we have an If Statement : 在这个块里面我们有一个If Statement

if(true)int i=0;

This statement, in turn, contains a local variable declaration: 反过来,该语句包含一个局部变量声明:

int i=0;

Therefore, the condition is violated. 因此,违反了该条件。 Recall: Every local variable declaration statement is immediately contained by a block. 回想一下: 每个局部变量声明语句都会立即被一个块包含。 However, in this case the local variable declaration is contained by an If statement, which is not a block itself, but is contained by another block. 但是,在这种情况下,局部变量声明包含在If语句中,该语句不是块本身,而是由另一个块包含。 Hence this code would not compile. 因此这段代码无法编译。

The only exception is for a for loop: 唯一的例外是for循环:

A local variable declaration can also appear in the header of a for statement (§14.14). In this case it is executed in the same manner as if it were part of a local variable declaration statement.

(You may need to reread it a few times to understand.) (您可能需要重读几次才能理解。)

Dark Falcon is right, if a single statement following an if test is a variable declaration then nothing can use that variable, the declaration would be limited in scope to the body of the if consequent, which would be useless, because anything following that would not have i in scope. 黑暗猎鹰是正确的,如果if测试之后的单个语句是变量声明,那么没有任何东西可以使用该变量,声明的范围将限制为if结果的主体,这将是无用的,因为任何后续的都不会i在范围内。

If if (true)int i = 0; 如果if (true)int i = 0; actually worked (meaning the declaration was visible to succeeding lines), that would mean that whether you had a variable declared would depend on the result of the if test, which would not be good. 实际工作(意味着声明对后续行可见),这意味着你是否有一个声明的变量将取决于if测试的结果,这将是不好的。

BTW, if (true) int i = 0; BTW, if (true) int i = 0; causes this syntax error in Eclipse: 在Eclipse中导致此语法错误:

Multiple markers at this line
    - i cannot be resolved to a variable
    - Syntax error on token "int", delete 

while putting the brackets around it compiles, although it generates a warning: 虽然它会产生警告,但是在它周围放置括号时会编译它们:

the value of the local variable i is not used

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM