“循环:"在 Java 代码中.这是什么,为什么要编译? [英] "loop:" in Java code. What is this, and why does it compile?

查看:25
本文介绍了“循环:"在 Java 代码中.这是什么,为什么要编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码让我盯着屏幕看了几分钟:

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

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

(此处为第 137 行)

我以前从未见过这个,我不知道 Java 有一个循环"关键字(NetBeans 甚至不会像关键字那样给它上色),而且它在 JDK 6 中编译得很好.

I have never seen this before, and I had no idea Java has a "loop" keyword (NetBeans doesn't even color it like a keyword), and it does compile fine with JDK 6.

解释是什么?

推荐答案

这不是一个keyword,而是一个label.

It is not a keyword it is a label.

用法:

    label1:
    for (; ; ) {
        label2:
        for (; ; ) {
            if (condition1) {
                // break outer loop
                break label1;
            }
            if (condition2) {
                // break inner loop
                break label2;
            }
            if (condition3) {
                // break inner loop
                break;
            }
        }
    }

文档.

这篇关于“循环:"在 Java 代码中.这是什么,为什么要编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆