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

查看:1562
本文介绍了为什么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天全站免登陆