为什么Java有“无法访问的语句"?编译器错误? [英] Why does Java have an "unreachable statement" compiler error?

查看:34
本文介绍了为什么Java有“无法访问的语句"?编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现在调试程序时,在代码块中插入 return 语句很方便(虽然可以说是不好的做法).我可能会在 Java 中尝试这样的事情......

I often find when debugging a program it is convenient, (although arguably bad practice) to insert a return statement inside a block of code. I might try something like this in Java ....

class Test {
        public static void main(String args[]) {
                System.out.println("hello world");
                return;
                System.out.println("i think this line might cause a problem");
        }
}

当然,这会产生编译器错误.

of course, this would yield the compiler error.

Test.java:7:无法访问的语句

Test.java:7: unreachable statement

我可以理解为什么警告可能会被证明具有未使用的代码是不好的做法.但我不明白为什么这需要产生错误.

I could understand why a warning might be justified as having unused code is bad practice. But I don't understand why this needs to generate an error.

这只是 Java 试图成为保姆,还是有充分的理由使它成为编译器错误?

Is this just Java trying to be a Nanny, or is there a good reason to make this a compiler error?

推荐答案

因为无法访问的代码对编译器毫无意义.虽然让代码对人们有意义比让代码对编译器有意义更重要也更难,但编译器是代码的基本消费者.Java 的设计者认为对编译器没有意义的代码是错误的.他们的立场是,如果您有一些无法访问的代码,那么您就犯了一个需要修复的错误.

Because unreachable code is meaningless to the compiler. Whilst making code meaningful to people is both paramount and harder than making it meaningful to a compiler, the compiler is the essential consumer of code. The designers of Java take the viewpoint that code that is not meaningful to the compiler is an error. Their stance is that if you have some unreachable code, you have made a mistake that needs to be fixed.

这里有一个类似的问题:无法访问的代码:错误或警告?,其中作者说我个人强烈认为这应该是一个错误:如果程序员编写一段代码,它应该始终具有在某些场景中实际运行它的意图."显然 Java 的语言设计者同意.

There is a similar question here: Unreachable code: error or warning?, in which the author says "Personally I strongly feel it should be an error: if the programmer writes a piece of code, it should always be with the intention of actually running it in some scenario." Obviously the language designers of Java agree.

不可达代码是否应该阻止编译是一个永远不会达成共识的问题.但这就是 Java 设计者这样做的原因.

Whether unreachable code should prevent compilation is a question on which there will never be consensus. But this is why the Java designers did it.

评论中的许多人指出,Java 不会阻止编译的无法访问的代码类有很多.如果我正确理解了哥德尔的后果,那么任何编译器都不可能捕获所有无法访问的代码类.

A number of people in comments point out that there are many classes of unreachable code Java doesn't prevent compiling. If I understand the consequences of Gödel correctly, no compiler can possibly catch all classes of unreachable code.

单元测试无法捕获每一个错误.我们不会将此用作反对其价值的论据.同样,编译器无法捕获所有有问题的代码,但在可能的情况下防止编译错误代码仍然很有价值.

Unit tests cannot catch every single bug. We don't use this as an argument against their value. Likewise a compiler can't catch all problematic code, but it is still valuable for it to prevent compilation of bad code when it can.

Java 语言设计者将无法访问的代码视为错误.所以在可能的情况下阻止它编译是合理的.

The Java language designers consider unreachable code an error. So preventing it compiling when possible is reasonable.

(在你拒绝之前:问题不在于 Java 是否应该有一个无法访问的语句编译器错误.问题是为什么 Java 有一个无法访问的语句编译器错误.不要因为我你认为 Java 做出了错误的设计决定.)

(Before you downvote: the question is not whether or not Java should have an unreachable statement compiler error. The question is why Java has an unreachable statement compiler error. Don't downvote me just because you think Java made the wrong design decision.)

这篇关于为什么Java有“无法访问的语句"?编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆