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

查看:198
本文介绍了在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 - 当是两个,它成功地将值打印为2.

场景2 - 当我要在<$ c中评论 value = 2 时$ c>案例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.

问题:


场景1:如果执行流程没有转到案例1:(当 key = 2 ),那么它如何知道值变量的类型为 int


场景2:如果编译器知道值变量的类型为 int ,那么它必须访问 int value = 1; 表达式案例1:。(声明和初始化)。那么为什么sqawrk当我要在案例2 中评论 value = 2 时,说局部变量值可能尚未初始化

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.

推荐答案

切换语句在范围方面是奇怪的,基本上。来自 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.

在你的情况下,案例2 案例1 位于同一中,并显示在其后面,即使 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天全站免登陆