在 Java 开关中声明和初始化变量 [英] Declaring and initializing variables within Java switches

查看:37
本文介绍了在 Java 开关中声明和初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Java 开关的疯狂问题.

I have a crazy question about Java switches.

int key = 2;

switch (key) {
    case 1:
        int value = 1;
        break;
    case 2:
        value = 2;
        System.out.println(value);
        break;
    default:
        break;
}

场景 1 - 当 key 为 2 时,它成功地将值打印为 2.
场景 2 - 当我要在 case 2 中评论 value = 2 时: 它大声说 局部变量值可能尚未初始化.

Scenario 1 - When the key is two it successfully print the value as 2.
Scenario 2 - When I'm going to comment value = 2 in case 2: it squawks saying the The local variable value may not have been initialized.

问题:

场景一:如果执行流程不去case 1:(当key = 2时),那它怎么知道类型int 的值变量?

场景2:如果编译器知道value变量的类型为int,那么它一定访问过int value = 1;中的表达式case 1:.(声明和初始化).那为什么它会在case 2:中注释value = 2,说局部变量值可能没有被初始化em>.

Questions :

Scenario 1 : If the execution flow doesn't go to case 1: (when the key = 2), then how does it know the type of the value variable as int?

Scenario 2 : If the compiler knows the type of the value variable as int, then it must have accessed to the int value = 1; expression in case 1:.(Declaration and Initialization). Then why does it sqawrk When I'm going to comment value = 2 in case 2:, saying the The local variable value may not have been initialized.

推荐答案

基本上,Switch 语句在范围方面很奇怪.来自 JLS 第 6.3 节:

Switch statements are odd in terms of scoping, basically. From section 6.3 of the JLS:

块中局部变量声明的范围(第 14.4 节)是该声明出现的块的其余部分,从它自己的初始值设定项开始,并包括局部变量声明语句右侧的任何其他声明符.

The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

在您的情况下,case 2case 1 位于同一个 block 并出现在它之后,即使 case 1 永远不会执行...所以局部变量在范围内并且可用于编写,尽管您在逻辑上从不执行"声明.(虽然初始化是,但声明并不是真正可执行的".)

In your case, case 2 is in the same block as case 1 and appears after it, even though case 1 will never execute... so the local variable is in scope and available for writing despite you logically never "executing" the declaration. (A declaration isn't really "executable" although initialization is.)

如果您注释掉 value = 2; 赋值,编译器仍然知道您指的是哪个变量,但您不会经过任何为其分配值的执行路径,这就是为什么当您尝试读取任何其他未确定分配的局部变量时会出现错误的原因.

If you comment out the value = 2; assignment, the compiler still knows which variable you're referring to, but you won't have gone through any execution path which assigns it a value, which is why you get an error as you would when you try to read any other not-definitely-assigned local variable.

我强烈建议您不要使用在其他情况下声明的局部变量 - 正如您所见,这会导致代码高度混乱.当我在 switch 语句中引入局部变量时(我很少尝试这样做 - 理想情况下,案例应该很短)我通常更喜欢引入一个新的作用域:

I would strongly recommend you not to use local variables declared in other cases - it leads to highly confusing code, as you've seen. When I introduce local variables in switch statements (which I try to do rarely - cases should be very short, ideally) I usually prefer to introduce a new scope:

case 1: {
    int value = 1;
    ...
    break;
}
case 2: {
    int value = 2;
    ...
    break;
}

我相信这更清楚.

这篇关于在 Java 开关中声明和初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆