“loop:” in Java code. What is this, and why does it compile?

后端 未结 12 1382
生来不讨喜
生来不讨喜 2020-11-22 08:02

This code just made me stare at my screen for a few minutes:

loop:
for (;;) {
    // ...
}

(line 137 here)

I have never seen this b

12条回答
  •  [愿得一人]
    2020-11-22 08:09

    It's a label, though look at the following example:

    int a = 0;
    int b = 0
    while (a<10){
        firstLoop:
        a++;
        while(true){
            b++
            if(b>10){
                break firstLoop;
            }
        }
     }
    

    When b>10 the execution flow goes to the outer loop.

提交回复
热议问题