[英]What is the meaning of this Java code?
It happened to me recently to write a piece of code similar to this one: 最近我碰巧写了一段与此类似的代码:
switch (x) {
case a: case b: case c:
// do something
break;
case d: case e: case f:
// do something
break;
}
Then, I got wrong and wrote a similar code with a syntax error: I forgot to write the case keyword: 然后,我错了,并写了一个类似的代码与语法错误:我忘了写case关键字:
switch (x) {
case a: b: c:
// do something
break;
case d: e: f:
// do something
break;
}
This syntax is actually valid in some other languages and the switch passes through all the values. 此语法实际上在某些其他语言中有效 ,并且交换机会传递所有值。
Actually here is valid too, as I did not get any syntax error: but a wrong behaviour by the program that was executing the switch smoothly, only missing the values without the case keyword. 实际上这里也是有效的,因为我没有得到任何语法错误:但是顺利执行切换的程序的错误行为,只缺少没有case关键字的值。
Why? 为什么? What does the b;
什么是b; c:, e: and f: mean in the second snip?
c:,e:和f:意味着第二次剪辑?
Are they maybe labels ? 它们可能是标签吗? And then, how could they possibly be on the same line?
然后,他们怎么可能在同一条线上呢? What am I missing that I don't understand behind this weird non-error?
我错过了什么,我不理解这个奇怪的非错误背后?
Edit: they appear to be indeed labels . 编辑:它们似乎确实是标签 。 It's unusual and as @Bathsheba explained, the problem is that labels are generally at the begin of a line.
这是不寻常的,正如@Bathsheba所解释的那样,问题在于标签通常是在一行的开头。 Weird how Java allows such a confusing (in my mind) syntax, without even showing a warning.
奇怪的是Java如何在我的脑海中使用这种令人困惑的语法,甚至没有显示警告。 This can create really a lot of confusion and weird errors in long programs.
这可能会在长程序中造成很多混乱和奇怪的错误。
Thank you. 谢谢。
In the second case, b:
, c:
, e:
, and f:
are labels . 在第二种情况下,
b:
, c:
, e:
和f:
是标签 。 You are allowed to break
to a label in Java, and the language allows you to insert a label in all sorts of places, including the places where you have. 您可以使用Java
break
标签,并且该语言允许您在各种位置插入标签,包括您拥有的位置。
It's just unusual to see labels not starting at the beginning of a line, that's all. 看到标签不是从一行开始就是不寻常的,这就是全部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.