为什么允许开关语法中的链接标签?它做什么?
我最近发现,在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,您有类似的东西:
barfoo:
不是开关情况。与开关机制无关。这只是一个标签。您可以标记任何语句,但并不总是有用的。它通常用于循环:例如,如果您实际上想将多种情况分组在一起,则可以像:
Jesper在评论中所提到的那样,Java的最新版本也支持A switch表达式,在其下是:
Where in your code you have something like this:
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.If you actually wanted to group together multiple cases, you could write it like:
And as Jesper mentions in the comments, more recent versions of Java also support a switch expression, under which the syntax is: