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

博客探讨了Java中为何在if语句中局部变量声明需要使用花括号。根据Java SE 7规范,局部变量声明必须直接位于块内。在给出的代码示例中,错误在于if语句后的局部变量声明没有包含在块中,导致编译错误。只有当该声明在for循环中时,才允许不使用花括号。
摘要由CSDN通过智能技术生成

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.

I'm using JGrasp by the way.

解决方案

Here is my understanding. Quoting from Chapter 14 of JAVA SE 7 specification:

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. 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.

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(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. Hence this code would not compile.

The only exception is for a for loop:

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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值