为什么允许开关语法中的链接标签?它做什么?

发布于 2025-01-24 03:14:05 字数 1451 浏览 0 评论 0原文

我最近发现,在Java Switch语句中,可以链接多个标签,但没有明显的效果。它甚至允许不存在的标签。例如,您可以这样做: case enum_value_1:enum_value_2:,甚至此case enum_value_1:foobar:barfoo:。只要以结肠结尾,任何价值似乎都被接受。 首先,尽管这可能是分组多种情况的另一种方法,但这似乎并不起作用。因此,我进行了以下测试

public class Main {

public enum TestEnum {
    A,
    B
}

public static void main(String[] args) {
    TestEnum testEnum = TestEnum.B;

    switch (testEnum) {
        case A: System.out.println("Test 1: Case 1"); break;
        case B: System.out.println("Test 1: Case 2"); break;
    }

    switch (testEnum) {
        case A: System.out.println("Test 2: Case 1"); break;
        case B:A: System.out.println("Test 2: Case 2"); break; // Label A is already used in enum
    }

    switch (testEnum) {
        case A:B: System.out.println("Test 3: Case 1"); break; // Label B is already used in enum
        case B: System.out.println("Test 3: Case 2"); break;
    }

    switch (testEnum) {
        case A:BARFOO: System.out.println("Test 4: Case 1"); break; // Label BARFOO does not exist
        case B:FOOBAR: System.out.println("Test 4: Case 2"); break; // Label FOOBAR does not exist
    }

    switch (testEnum) {
        case A:B: System.out.println("Test 5: Case 1"); break;
    }
}}

,我在Java 11和17中运行了它,两个版本的输出是:

Test 1: Case 2
Test 2: Case 2
Test 3: Case 2
Test 4: Case 2

我希望只有测试1和5实际编译,但是它们都进行了编译,但似乎实际标签之后的所有内容都是被忽略。

我怀疑这是一个疏忽,所以我想念什么?

I recently discovered, that in a java switch statement, multiple labels can be chained, but for no apparent effect. And it even allows labels that do not exist. For example you can do this:
case ENUM_VALUE_1:ENUM_VALUE_2:, or even this case ENUM_VALUE_1:foobar:barfoo:. As long as it ends with a colon, any value seems to be accepted.
First I though this might be a different way of grouping multiple cases, but that does not seem to be working. So I ran the following test

public class Main {

public enum TestEnum {
    A,
    B
}

public static void main(String[] args) {
    TestEnum testEnum = TestEnum.B;

    switch (testEnum) {
        case A: System.out.println("Test 1: Case 1"); break;
        case B: System.out.println("Test 1: Case 2"); break;
    }

    switch (testEnum) {
        case A: System.out.println("Test 2: Case 1"); break;
        case B:A: System.out.println("Test 2: Case 2"); break; // Label A is already used in enum
    }

    switch (testEnum) {
        case A:B: System.out.println("Test 3: Case 1"); break; // Label B is already used in enum
        case B: System.out.println("Test 3: Case 2"); break;
    }

    switch (testEnum) {
        case A:BARFOO: System.out.println("Test 4: Case 1"); break; // Label BARFOO does not exist
        case B:FOOBAR: System.out.println("Test 4: Case 2"); break; // Label FOOBAR does not exist
    }

    switch (testEnum) {
        case A:B: System.out.println("Test 5: Case 1"); break;
    }
}}

I ran it in java 11 and 17 and the output in both versions is:

Test 1: Case 2
Test 2: Case 2
Test 3: Case 2
Test 4: Case 2

I would have expected that only test 1 and 5 actually compile, but they all compile and it seems that everything after the actual label is just ignored.

I doubt this is an oversight, so what am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2025-01-31 03:14:05

在您的代码中,您有类似的东西:

case A:BARFOO:

barfoo:不是开关情况。与开关机制无关。这只是一个标签。您可以标记任何语句,但并不总是有用的。它通常用于循环:例如,

outerloop:
while (true) {
    while (true) {
        break outerloop;
    }
}

如果您实际上想将多种情况分组在一起,则可以像:

case A: case B:
    ...
    break;

Jesper在评论中所提到的那样,Java的最新版本也支持A switch表达式,在其下是:

case A, B -> ...

Where in your code you have something like this:

case A:BARFOO:

BARFOO: isn't a switch case. It's nothing to do with the switch mechanics. It's just a label. You're allowed to label any statement, but it's not always useful. It's typically used for loops: e.g.

outerloop:
while (true) {
    while (true) {
        break outerloop;
    }
}

If you actually wanted to group together multiple cases, you could write it like:

case A: case B:
    ...
    break;

And as Jesper mentions in the comments, more recent versions of Java also support a switch expression, under which the syntax is:

case A, B -> ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文