为什么 Java 编译器会抱怨局部变量没有在这里初始化?

发布于 2024-08-23 15:02:12 字数 352 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

并安 2024-08-30 15:02:12

如果将第二个 if 更改为 else,那么编译器会很高兴。

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

如果您确实想深入研究这个问题,Java 语言规范的一整章专门讨论 明确分配。此案例属于您的具体示例:

规则不接受变更:

void flow(布尔标志){
        整数 k;
        如果(标志)
                k = 3;
        如果(!标志)
                k = 4;
        System.out.println(k); // k 在此之前不是“明确分配”的
}

因此编译该程序必定会导致发生编译时错误。

这个特定的示例(以及许多其他说明性的示例)可能看起来超出了您的期望,但这正是语言设计者想要的方式,并且所有编译器都必须遵守规则。

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

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:

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.

久随 2024-08-30 15:02:12

重点关注“IF”,编译器无法判断条件是否为真。

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

喜你已久 2024-08-30 15:02:12

当代码甚至无法编译时,请不要尝试运行它。

通常您无论如何都无法做到这一点,但现代 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.

画离情绘悲伤 2024-08-30 15:02:12

在Java中,局部变量在使用之前必须进行初始化。

在您的情况下,两个初始化都是有条件的,编译器无法确定任何条件是否为真。这会让编译器感到不安。

来自 Java 文档

局部变量(第 14.4 节、第 14.13 节)必须在使用前通过初始化(第 14.4 节)或赋值(第 15.26 节)显式赋予值,编译器可以使用以下方式进行验证:明确赋值规则

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:

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

飘过的浮云 2024-08-30 15:02:12

局部变量在编译时不会被替换,因此编译器不知道 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.

尝蛊 2024-08-30 15:02:12

Java 编译器无法发现另一个 ifelse 一样工作。编译器很聪明,但没那么聪明。

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

空名 2024-08-30 15:02:12

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

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

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

// this compiles just fine
final int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文