Multiple returns: Which one sets the final return value?(多重返回:哪一个设置最终返回值?)
问题描述
鉴于此代码:
String test() {
try {
return "1";
} finally {
return "2";
}
}
语言规范是否定义了调用 test()
的返回值?换句话说:在每个 JVM 中它总是相同的吗?
Do the language specifications define the return value of a call to test()
? In other words: Is it always the same in every JVM?
在 Sun JVM 中,返回值为 2
,但我想确定,这与 VM 无关.
In the Sun JVM the return value is 2
, but I want to be sure, that this is not VM-dependant.
推荐答案
是的,语言规范 定义2"是结果.如果 VM 的做法有所不同,则说明它不符合规范.
Yes, the language spec defines that "2" is the result. If a VM does it differently, it's not spec-compliant.
大多数编译器都会抱怨它.例如 Eclipse 会声称返回块永远不会被执行,但这是错误的.
Most compilers will complain about it. Eclipse, for example, will claim that the return block will never be executed, but it's wrong.
编写这样的代码是非常糟糕的做法,永远不要这样做:)
It's shockingly bad practice to write code like that, don't ever do it :)
这篇关于多重返回:哪一个设置最终返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多重返回:哪一个设置最终返回值?
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01