简体   繁体   English

Java未初始化的变量错误

[英]Java uninitialized variable error

In the code below, the Java compiler gives me an error in the line calling hasAdditionalLiberty , saying that x0 and y0 "may have not been initialized". 在下面的代码中,Java编译器在调用hasAdditionalLiberty的行中给了我一个错误,说x0y0 “可能尚未初始化”。 I do understand the intent of making this an error, but isn't that in this case, x0 and y0 must be initialized because they always go through the for loop? 我确实知道使错误发生的意图,但是不是在这种情况下必须初始化x0y0因为它们总是经过for循环吗?

So what exactly is the rule deciding the uninitialized variable error? 那么,决定未初始化变量错误的规则到底是什么?

int x0;
int y0;
for (int i = 0; i < 4; ++i) {
    x0 = x + deltaX[i];
    y0 = y + deltaY[i];
    if (isOnBoard(x0, y0) && at(x0, y0) == Color.Empty) {
        break;
    }
}
if (!hasAdditionalLiberty(x, y, x0, y0)) {
    koX = x0;
    koY = y0;
}

Well, the compiler does not know everything about the values of your variables through the program flow. 好吧,编译器并不了解程序流中有关变量值的所有信息。

Then, the compiler does not know if the program will enter the loop at least one time, so in which case x0 and y0 will not have been initialised. 然后,编译器不知道程序是否至少会进入循环一次,因此在这种情况下, x0y0不会被初始化。

If you have a loop with condition that depends of a variable: who does it know before the runTime if you will enter the loop? 如果您的循环条件取决于变量:在运行时之前谁知道您是否会进入循环?

In that case you will have an unexpected error in 在这种情况下,您将在

if (!hasAdditionalLiberty(x, y, x0, y0)) {
    koX = x0;
    koY = y0;
}

You only have to itialize then with 您只需要用

int x0 = -1;
int y0 = -1;

or 要么

int x0 = 0;
int y0 = 0;

for example. 例如。

Yes, I know that in your case you have a "static" loop because of the condition is < 4 , soy may if someone develops a different compiler to recognize this, you won't have that error. 是的,我知道在您的情况下,由于条件< 4 ,您将出现“静态”循环,因此如果有人开发其他编译器来识别此问题,大豆可能不会出现该错误。

For further information about when you get this type of error, read this . 有关何时出现此类错误的更多信息,请阅读this

The loop isn't guaranteed (in the compiler's opinion) to have made one iteration (since the compiler doesn't do sufficient analysis of the for loop expressions). (因为编译器没有对for循环表达式进行足够的分析,所以不能保证循环(在编译器看来)进行了一次迭代。

Refer to JLS, Chapter 16 for the rule deciding the uninitialized variable error: 有关确定未初始化变量错误的规则请参阅JLS,第16章

The analysis takes into account the structure of statements and expressions […, however] values of expressions are not taken into account in the flow analysis. 该分析考虑了语句和表达式的结构[…,但是,在流分析中未考虑表达式的值 (My emphasis.) (我的重点。)

The compiler isn't smart enough to know for sure that the code will enter the loop. 编译器不够聪明,无法确定代码将进入循环。 Even if you can tell that the code will enter the loop, the code can't. 即使可以告诉代码将进入循环,代码也不会。 That's not what a compiler is for. 那不是编译器的目的。

Consider this code: 考虑以下代码:

public static void main(String... args){
   int x;
   if(Math.random() < .5){
      x = 100;
   }
   System.out.println(x);
}

This code will generate the same compiler error, for the same exact reason. 出于相同的确切原因,此代码将生成相同的编译器错误。

The compiler doesn't know if your for loop will definitely run or not. 编译器不知道您的for循环是否一定会运行。 To prevent the warning, just initialize your variables. 为了避免警告,只需初始化变量即可。

int x0 = 0;
int y0 = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM