且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用intellij编译器错误

更新时间:2021-08-07 21:45:14

Java编译器不够聪明,无法证明变量在执行初始化变量的代码之后,您将永远不会 1001 。请记住,Java变量声明是完全静态的;按照设计,Java只允许您的变量以有意义的方式使用,即在使用前进行初始化。并且证明这种情况发生,对于一般代码,相当于解决暂停问题。 (对于编译器知道的所有内容,表达式 I.compareTo(TMP_1)> 0 可能是无意义的,因为它引用了一个不存在的变量。(更确切地说,变量在开关语句的主体范围内声明,但如果跳到标签案例1001:。))

The Java compiler isn't smart enough to prove that the variable you're switching on will never be 1001 until after the code that initializes the variable is executed. Remember that Java variable declarations are completely static; by design, Java only allows your variable to be used in ways that make sense, i.e. are initialized before use. And proving that this happens, for general code, is equivalent to solving the halting problem. (For all that the compiler knows, the expression I.compareTo(TMP_1) > 0 could be nonsense, since it refers to a nonexistent variable. (More precisely, the variable is declared in the scope of the switch statement's body, but the code that initializes it would not execute if you skip to the label case 1001:.))

您不能将此错误转变为警告;这是静态语言的缺点之一。特别是 Java语言规范,第16章要求:

You aren't permitted to turn this error into a warning; that's one of the drawbacks of a static language. In particular, the Java Language Specification, chapter 16 requires:


对于本地变量x的每次访问,x必须在访问之前明确赋值,或者发生编译时错误。

For every access of a local variable [...] x, x must be definitely assigned before the access, or a compile-time error occurs.

并且在访问之前变量不是明确赋值(在规范中定义)。 IntelliJ使用Java编译器(通常是javac)编译代码。因为你要做的事情需要是标准的错误,你想要的是不可能的(没有编辑编译器,然后它就不再是Java了。)

and the variable is not "definitely assigned" (as defined in the spec) before access. IntelliJ compiles your code using a Java compiler (usually javac). Since what you're trying to do is required to be an error by the standard, what you want is impossible (short of editing the compiler, and then it wouldn't be Java anymore).

相反,只需在周围范围内声明变量,并将其初始化为虚拟值。例如:

Instead, simply declare your variable in the surrounding scope, and initialize it to a dummy value. For example:

Double TMP_1 = null;
while(exitVar) {
    switch(lblToGoTo) {
        ...
        case 1000:
            TMP_1 = len(T$);
            I = 1d;
        case 1001:
            if (I.compareTo(TMP_1) > 0) { ... }
        ...
    }
}