为什么 Java 编译器会抱怨局部变量没有在这里初始化?
int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);
如果我运行此命令,我会收到:
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)
我知道局部变量未初始化,并且您有责任执行此操作,但在这种情况下,第一个 if 未初始化变量?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果将第二个
if
更改为else
,那么编译器会很高兴。如果您确实想深入研究这个问题,Java 语言规范的一整章专门讨论 明确分配。此案例属于您的具体示例:
这个特定的示例(以及许多其他说明性的示例)可能看起来超出了您的期望,但这正是语言设计者想要的方式,并且所有编译器都必须遵守规则。
If you change the second
if
toelse
, then the compiler would be happy.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:
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.
重点关注“IF”,编译器无法判断条件是否为真。
Focus on "IF", The compiler can't tell if the condition will be true.
当代码甚至无法编译时,请不要尝试运行它。
通常您无论如何都无法做到这一点,但现代 IDE 非常“有帮助”,可以让您做到这一点。他们通常用只会抛出错误的代码替换代码中不可编译的部分,例如您所看到的错误。
更好的方法是查看编译器/IDE 提供的错误消息,并在尝试运行应用程序之前尝试修复这些错误消息。
了解编译器错误和运行时异常之间的区别是重要的一步。
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.
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.
Learning the difference between compiler errors and runtime exceptions is an important step to take.
在Java中,局部变量在使用之前必须进行初始化。
在您的情况下,两个初始化都是有条件的,编译器无法确定任何条件是否为真。这会让编译器感到不安。
来自 Java 文档:
In Java a local variable must be initialized before its used.
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:
局部变量在编译时不会被替换,因此编译器不知道
IF
是 true 还是 false。另一方面,如果变量被定义为
final
,那么它将在编译时被替换。Local variables are not replaced in compile time so the compiler have no idea if the
IF
is true or false.On the other hand if the variable was defined
final
then it will be replaced during compile time.Java 编译器无法发现另一个
if
与else
一样工作。编译器很聪明,但没那么聪明。Java compiler cannot find out that the other
if
works as anelse
. Compilers are smart but not that smart.您可以将
final
关键字添加到a
的声明中,以帮助编译器优化代码。You could add the
final
keyword to the declaration ofa
to help your compiler optimizing the code.