- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定以下有效代码:
Boolean a = false;
if (a)
System.out.println("A");
else
System.out.println("!A");
根据documentation , if
包括它的条件和内部的 statement
也是一个 statement
。例如:
if (b){
System.out.println("B");
}
是一个语句
。
但是,当我们要用另一个语句替换现有语句时,它不应该触及整体逻辑,对吧?假设我们将 (Expression)-statement
替换为上面的 if-statement
:
Boolean a = false;
Boolean b = false:
if (a)
if (b){
System.out.println("A and B");
}
else
System.out.println("!A");
Java Compiler 将解释代码示例如下(完整的括号解释):
Boolean a = false;
Boolean b = false:
if (a){
if (b){
System.out.println("A and B");
} else {
System.out.println("!A");
}
}
这不是最初的逻辑。
那么为什么将一个语句与另一个语句交换会改变逻辑?
从示例中可以清楚地看出,问题出在 大括号 上,但如果省略大括号,我找不到有关 java 准确处理此问题的方式的信息。
是否有关于此行为的任何记录?为什么 java 更喜欢将 else
连接到最近的 if
,而不是它在解析时遇到的第一个 if
?
最佳答案
当然,答案在Java Language Specification中.相关部分是section 14.5, "Statements" ,它恰好描述了这种情况:
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"The problem is that both the outer
if
statement and the innerif
statement might conceivably own theelse
clause. In this example, one might surmise that the programmer intended theelse
clause to belong to the outerif
statement.
最后:
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语句,处理优先级("dangling else"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076762/
你认为这是一个悬空else的情况吗? 根据编译器和印度理工学院教授的说法,确实如此。但我有疑问!从理论上讲,在满足 if 的条件后,它总是只会进一步处理一条语句(或者一个复合语句,即括号内的多个语句)
我正在使用 dangle.js,这是一个基于 D3 为 Angular JS 构建的可视化工具。我正在尝试通过使用 html 调用指令来输出图形 在我的 Controller 中我有代码 $scop
给定以下有效代码: Boolean a = false; if (a) System.out.println("A"); else System.out.println("!A"); 根据
我想用 jQuery 或更好的纯 javascript 创建一个动画,使 div “悬垂”。我附上了显示动画的动画 gif。我不知道如何重新创建它,如果它是我可以使用现有的 jquery 缓动/动画或
我正在为 JavaFX 创建一个带有登录名和用户名的识别窗口,我正在使用正则表达式来确保密码至少包含一个特殊字符和一位数字。 这是我正在使用的正则表达式 newValue.matches("*\\d+
Git的 fsck doc谈论“悬空”对象,而 the gc doc只谈论“松散的物体”。有严格的划分。 但是在浏览一些相关的 SO 帖子时,这些术语似乎可以互换使用。在 Git Book v2还有
我经常看到悬空的 else 处理为: if (x > 0) if (y > 0) print "hello" else print "world" 解析器或解释器实际上会将 else
考虑以下问题:我有许多类,每个类都实现一个 get() 函数。以下 container1 和 container2 是此类的示例: struct expensive_type { int v;
我想构建一个模式来将逗号插入整数以使其看起来不错,例如 1234556 ,操作后,就会像 1,234,556 ,所以我使用以下代码: public static void insertCommaFor
在 Javascript 中,如果您引用了某个 DOM 元素,然后通过向 document.body 添加其他元素来修改 DOM,您的 DOM 引用就会失效。 考虑以下代码: function
这个问题在这里已经有了答案: Can a local variable's memory be accessed outside its scope? (20 个答案) 关闭 7 年前。 这可能是一
由于我更新了我的 Android Studio (2.3.1) 和构建工具 (2.3.1),我收到了警告, Warning: Dangling Javadoc comment 对于像这样的评论, /*
在 Scala 中,可以在函数中定义局部 block 。本地 block 计算最后的语句,例如, val x = {val x =1;x+1} 此处 x==2,内部 val x 是该 block 的本
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
我希望我的 node.js 测试能够确保,一旦测试结束并且 test.finish() 或类似的调用被调用,就没有更多的代码等待运行。没有更多的 I/O 等待完成,没有更多的定时器等待触发,等等。 这
我知道有些人认为前导下划线的存在暗示变量是“私有(private)的”,这种隐私是虚构的,并假设这就是为什么 JSLint 报告带有错误消息的此类名称。 我在我正在构建的网站上使用 Google An
我正在开发一个 Cocoa-Touch 应用程序,它使用 CoreData 并具有一些由 XCode 模型编辑器生成的 NSPersistentObject 子类。 我注意到最近,在保存上下文时,我收
我在使用 Core Data 时遇到了一个非常令人困惑的问题。在我的核心数据存储中,对于现有的核心数据对象,我检查是否存在关系,如果不存在,我将像这样创建对象(这是 AFFingerprintGene
在另一个question ,我发现Assigned()功能与 Pointer <> nil 相同。我一直以来的理解是Assigned()正在检测这些悬空指针,但现在我知道它没有。悬空指针是那些可能在某
我在 iOS 上遇到以下崩溃: Unresolved error Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn
我是一名优秀的程序员,十分优秀!