Java语句,处理优先级(“悬挂其他”) [英] Java Statements, processing precedence ("dangling else")
问题描述
给出以下有效代码:
Boolean a = false;
if (a)
System.out.println("A");
else
System.out.println("!A");
现在,根据文件 if
包括它的条件和内部语句
也是一个语句
( https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html - 至少它被称为 if-statement
遍及整个文档)
Now, according to the documentation if
including it's condition and inner statement
is also a statement
(https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html - at least it's called if-statement
all over the documentation)
例如:
if (b){
System.out.println("B");
}
是语句
。
但是,当我们要用另一个语句替换现有语句时,它不应该触及整体逻辑,对吧?假设我们用上面的 if-statement
替换(Expression) - 语句
:
However, when we are going to replace an existing statement with another one, it shouldn't touch the overall logic, right? Assuming we replace the (Expression)-statement
with the if-statement
above :
Boolean a = false;
Boolean b = false:
if (a)
if (b){
System.out.println("A and B");
}
else
System.out.println("!A");
Java编译器会将代码示例解释如下(完整的大括号用于解释):
Java Compiler will interpret the code example as follows (full braces for explanation):
Boolean a = false;
Boolean b = false:
if (a){
if (b){
System.out.println("A nad B");
} else {
System.out.println("!A");
}
}
这不是初始逻辑。
那么为什么要交换一个语句来改变逻辑?
So why is exchanging one statement against another changing the logic?
来自示例可以清楚地说,问题只是关于大括号,但是我无法找到有关java正是如何处理这个问题的信息,如果有大括号的话。
From the example one can clearly say, that the problem is just about the braces, but I cannot find information about the way java is exactly dealing with this, if braces are ommited.
因此,如果有关于此行为的任何记录,我只是感兴趣吗?为什么java更喜欢将 else
连接到更新的 if
,而不是第一个如果
它在解析时会遇到吗?
Therefore I'm just interested if there is anything written down about THIS behavior? Why does java prefer to connect the else
to the more recent if
, rather than the first if
it encounters while parsing?
推荐答案
当然,答案是在 Java语言规范。相关部分是第14.5节,声明,它描述了这种情况:
Of course, the answer is in the Java Language Specification. The relevant section is section 14.5, "Statements", which describes exactly this case:
在C和C ++中,Java编程语言的if语句受到影响所谓的悬空
否则
问题,这个误导性格式的例子说明了:
As in C and C++, the if statement of the Java programming language suffers from the so-called "dangling
else
problem," illustrated by this misleadingly formatted example:
if (door.isOpen())
if (resident.isVisible())
resident.greet("Hello!");
else door.bell.ring(); // A "dangling else"
问题是外部如果
语句和内部 if
语句可能会拥有 else
子句。在这个例子中,人们可能会猜测程序员希望 else
子句属于外部 if
语句。
The problem is that both the outer if
statement and the inner if
statement might conceivably own the else
clause. In this example, one might surmise that the programmer intended the else
clause to belong to the outer if
statement.
最后:
Java编程语言,如C和C ++以及它们之前的许多编程语言,任意命令
else
子句属于最里面的如果
可能属于。
The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an
else
clause belongs to the innermostif
to which it might possibly belong.
(由我强调)
这篇关于Java语句,处理优先级(“悬挂其他”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!