SCJP with label

2019-08-16 16:39发布

Below will be compilation fail due to "label z is missing" but if I just move z: to one step below after o = o + 2 then that will work? What is the logic behind this?

public class Breaker {
static String o = "";

public static void main(String[] args) {
z: 
o = o + 2;
for (int x = 3; x < 8; x++) {
    if (x == 4)
    break;
    if (x == 6)
    break z;
    o = o + x;
}
System.out.println(o);
}
}

标签: java scjp
1条回答
乱世女痞
2楼-- · 2019-08-16 17:17

You cannot put the labels everywhere in the code. it should be only before statements. in this case labelname: for(;;){} Here's the documentation

查看更多
登录 后发表回答