为什么Java编译器会抱怨本地变量没有被初始化?

时间:2023-01-07 01:43:51
int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);

If I run this, I receive:

如果我运行这个,我收到:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The local variable b may not have been initialized

 at Broom.main(Broom.java:9)

I know that the local variables are not initialized and is your duty to do this, but in this case, the first if doesn't initialize the variable?

我知道局部变量没有初始化,你有责任这么做,但是在这种情况下,第一个如果不初始化变量?

7 个解决方案

#1


14  

If you change the second if to else, then the compiler would be happy.

如果将第二个If改为else,编译器会很高兴。

int a = 1, b;
if(a > 0) b = 1;
else b = 2;
System.out.println(b);

If you really want to go deep into this matter, one whole chapter of the Java Language Specification is dedicated to the issue of Definite Assignment. This case pertains to your specific example:

如果您真的想深入研究这个问题,那么Java语言规范的整整一章专门讨论明确的分配问题。这个案例与你的具体例子有关:

the rules do not accept the variation:

规则不接受变动:

void flow(boolean flag) {
        int k;
        if (flag)
                k = 3;
        if (!flag)
                k = 4;
        System.out.println(k);  // k is not "definitely assigned" before here
}

and so compiling this program must cause a compile-time error to occur.

因此编译这个程序必须导致编译时错误。

This particular example (and many other illustrative ones) may seem to defy your expectation, but this is exactly the way the language designers wanted it, and all compilers must abide by the rules.

这个特定的示例(以及许多其他说明性的示例)似乎违背了您的期望,但这正是语言设计人员所希望的,所有编译器都必须遵守规则。

#2


5  

Focus on "IF", The compiler can't tell if the condition will be true.

集中在“如果”上,编译器无法判断条件是否为真。

#3


3  

Please don't try to run your code when it doesn't even compile.

请不要试图在它不编译时运行您的代码。

Usually you couldn't do that anyway, but modern IDEs are so "helpful" to allow you to do that. They usually replace uncompilable parts of the code with code that just throws an error such as the one you see.

通常无论如何您都不能这样做,但是现代ide非常“有用”,允许您这样做。它们通常用只会抛出错误(如您看到的错误)的代码替换代码中不可编译的部分。

The much better approach is to look at the error messages your compiler/IDE gives you and try to fix those before you try to run your application.

更好的方法是查看编译器/IDE提供的错误消息,并尝试在运行应用程序之前修复它们。

Learning the difference between compiler errors and runtime exceptions is an important step to take.

了解编译器错误和运行时异常之间的区别是需要采取的重要步骤。

#4


2  

In Java a local variable must be initialized before its used.

在Java中,必须在使用本地变量之前对其进行初始化。

In your case both the initializations are conditional and the compiler cannot determine if any of the conditionals will be true. This upsets the compiler.

在您的情况中,初始化都是有条件的,编译器不能确定是否有任何条件是正确的。这让编译器。

From Java docs:

从Java文档:

A local variable (§14.4, §14.13) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified by the compiler using the rules for definite assignment

一个局部变量(§14.4,§14.13)必须显式地给出一个值在使用它之前,通过初始化(§14.4)或任务(§15.26),可以验证的方式由编译器使用的规则明确的任务

#5


1  

Local variables are not replaced in compile time so the compiler have no idea if the IF is true or false.

在编译时不替换局部变量,因此编译器不知道if是真还是假。

On the other hand if the variable was defined final then it will be replaced during compile time.

另一方面,如果变量定义为final,那么它将在编译期间被替换。

#6


1  

Java compiler cannot find out that the other if works as an else. Compilers are smart but not that smart.

Java编译器无法发现另一个if作为一个else起作用。编译器很聪明,但没那么聪明。

#7


0  

You could add the final keyword to the declaration of a to help your compiler optimizing the code.

您可以向a的声明中添加final关键字,以帮助编译器优化代码。

// this compiles just fine
final int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);

#1


14  

If you change the second if to else, then the compiler would be happy.

如果将第二个If改为else,编译器会很高兴。

int a = 1, b;
if(a > 0) b = 1;
else b = 2;
System.out.println(b);

If you really want to go deep into this matter, one whole chapter of the Java Language Specification is dedicated to the issue of Definite Assignment. This case pertains to your specific example:

如果您真的想深入研究这个问题,那么Java语言规范的整整一章专门讨论明确的分配问题。这个案例与你的具体例子有关:

the rules do not accept the variation:

规则不接受变动:

void flow(boolean flag) {
        int k;
        if (flag)
                k = 3;
        if (!flag)
                k = 4;
        System.out.println(k);  // k is not "definitely assigned" before here
}

and so compiling this program must cause a compile-time error to occur.

因此编译这个程序必须导致编译时错误。

This particular example (and many other illustrative ones) may seem to defy your expectation, but this is exactly the way the language designers wanted it, and all compilers must abide by the rules.

这个特定的示例(以及许多其他说明性的示例)似乎违背了您的期望,但这正是语言设计人员所希望的,所有编译器都必须遵守规则。

#2


5  

Focus on "IF", The compiler can't tell if the condition will be true.

集中在“如果”上,编译器无法判断条件是否为真。

#3


3  

Please don't try to run your code when it doesn't even compile.

请不要试图在它不编译时运行您的代码。

Usually you couldn't do that anyway, but modern IDEs are so "helpful" to allow you to do that. They usually replace uncompilable parts of the code with code that just throws an error such as the one you see.

通常无论如何您都不能这样做,但是现代ide非常“有用”,允许您这样做。它们通常用只会抛出错误(如您看到的错误)的代码替换代码中不可编译的部分。

The much better approach is to look at the error messages your compiler/IDE gives you and try to fix those before you try to run your application.

更好的方法是查看编译器/IDE提供的错误消息,并尝试在运行应用程序之前修复它们。

Learning the difference between compiler errors and runtime exceptions is an important step to take.

了解编译器错误和运行时异常之间的区别是需要采取的重要步骤。

#4


2  

In Java a local variable must be initialized before its used.

在Java中,必须在使用本地变量之前对其进行初始化。

In your case both the initializations are conditional and the compiler cannot determine if any of the conditionals will be true. This upsets the compiler.

在您的情况中,初始化都是有条件的,编译器不能确定是否有任何条件是正确的。这让编译器。

From Java docs:

从Java文档:

A local variable (§14.4, §14.13) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified by the compiler using the rules for definite assignment

一个局部变量(§14.4,§14.13)必须显式地给出一个值在使用它之前,通过初始化(§14.4)或任务(§15.26),可以验证的方式由编译器使用的规则明确的任务

#5


1  

Local variables are not replaced in compile time so the compiler have no idea if the IF is true or false.

在编译时不替换局部变量,因此编译器不知道if是真还是假。

On the other hand if the variable was defined final then it will be replaced during compile time.

另一方面,如果变量定义为final,那么它将在编译期间被替换。

#6


1  

Java compiler cannot find out that the other if works as an else. Compilers are smart but not that smart.

Java编译器无法发现另一个if作为一个else起作用。编译器很聪明,但没那么聪明。

#7


0  

You could add the final keyword to the declaration of a to help your compiler optimizing the code.

您可以向a的声明中添加final关键字,以帮助编译器优化代码。

// this compiles just fine
final int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);