Java 标签用法

标签 java

<分区>

在某处浏览 java 优秀文章时,我发现这样的代码可以完美编译。

public int myMethod(){
    http://www.google.com
    return 1;
}

描述说 http: 词将被视为标签和 //www.google.com 被视为评论

我不明白 Java Label 在外部循环中有何用处? 什么情况下应该使用Java Label外循环?

最佳答案

这是在 Java 中使用标签的一个好处:

block:
{
    // some code

    if(condition) break block;

    // rest of code that won't be executed if condition is true
}

嵌套循环的另一种用法:

outterLoop: for(int i = 0; i < 10; i++)
{
    while(condition)
    {
        // some code

        if(someConditon) break outterLoop; // break the for-loop
        if(anotherConditon) break; // break the while-loop

        // another code
    }

    // more code
}

或:

outterLoop: for(int i = 0; i < 10; i++)
{
    while(condition)
    {
        // some code

        if(someConditon) continue outterLoop; // go to the next iteration of the for-loop
        if(anotherConditon) continue; // go to the next iteration of the while-loop

        // another code
    }

    // more code
}

关于Java 标签用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836549/

相关文章:

java - 不绕过 jdbc 的数据库设计

java - 非目标绑定(bind)与 `toInstance()` 绑定(bind)

java - 如何正确引用 Spinner 条目?

java - 如何使用 applicationContext.xml 中设置的一些默认值从 Spring 中的 JVM 选项访问属性值?

java - 选择: but not including :之后 ""(含)内的所有文本

java - 如何在java中重置数据库表中的id?

java - 我可以从静态内部类访问外部类的字段吗?

java - 将数组中字符串的递增数字连接起来

java - Cobertura 改变了 Sonar 违规行为

java - Modelio 类图隔间