美文网首页
Java基础—break label 带标签的break语句的用

Java基础—break label 带标签的break语句的用

作者: 苡仁ilss | 来源:发表于2019-03-10 14:20 被阅读0次
    • 是这样的,今天看RocketMQ的Client的时候,里面出现了一个写Java没有看到过的东西 - - 记录一下。
    • 也是就是带标签的break;
      RocketMQ的Client中的使用
    while(true) {
                    label122: {
                                   switch(communicationMode) {
                                    case ASYNC:
                                        return null;
                                    case ONEWAY:
                                        return null;
                                    case SYNC:
                                        if () {
                                            return sendResult;
                                        }
                                    default:
                                        break label122;
                                    }
                              }
               }
    
    • 其实很容易理解:就是标记代码块为label122 当执行到default的时候break到代码块执行。
    • 怎么用呢?
    package io.ilss.rocketmq.demo;
    
    /**
     * className Main
     * description Main
     *
     * @author feng
     * @version 1.0
     * @date 2019-03-10 13:45
     */
    public class Main {
        public static void main(String[] args) {
            label1:
            for (int i = 0; i < 10; i++) {
                label2:
                if (i > 0) {
                    if (i > 8) {
                        break label1;
                    }
                    System.out.print(i + " ");
                    if (i > 3) {
                        break label2;
                    }
                    System.out.print(" if end ");
    
                }
    
                label3: {
                    if (i > 5) {
                        break label3;
                    }
                    System.out.print(", label3 block");
                }
                System.out.println(", for end ");
            }
        }
    }
    
    
    • 以下是输出结果:
    /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=52397:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/tools.jar:/Users/feng/Projects/idea/springboot-rocketmq-ilss/target/classes:/Users/feng/Developer/mvn-repository/org/springframework/boot/spring-boot-starter/2.1.3.RELEASE/spring-boot-starter-2.1.3.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/boot/spring-boot/2.1.3.RELEASE/spring-boot-2.1.3.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-context/5.1.5.RELEASE/spring-context-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-aop/5.1.5.RELEASE/spring-aop-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-beans/5.1.5.RELEASE/spring-beans-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-expression/5.1.5.RELEASE/spring-expression-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/boot/spring-boot-autoconfigure/2.1.3.RELEASE/spring-boot-autoconfigure-2.1.3.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/boot/spring-boot-starter-logging/2.1.3.RELEASE/spring-boot-starter-logging-2.1.3.RELEASE.jar:/Users/feng/Developer/mvn-repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/feng/Developer/mvn-repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/feng/Developer/mvn-repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.2/log4j-to-slf4j-2.11.2.jar:/Users/feng/Developer/mvn-repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar:/Users/feng/Developer/mvn-repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Users/feng/Developer/mvn-repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-core/5.1.5.RELEASE/spring-core-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/springframework/spring-jcl/5.1.5.RELEASE/spring-jcl-5.1.5.RELEASE.jar:/Users/feng/Developer/mvn-repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/Users/feng/Developer/mvn-repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/feng/Developer/mvn-repository/org/apache/rocketmq/rocketmq-client/4.4.0/rocketmq-client-4.4.0.jar:/Users/feng/Developer/mvn-repository/org/apache/rocketmq/rocketmq-common/4.4.0/rocketmq-common-4.4.0.jar:/Users/feng/Developer/mvn-repository/org/apache/rocketmq/rocketmq-remoting/4.4.0/rocketmq-remoting-4.4.0.jar:/Users/feng/Developer/mvn-repository/com/alibaba/fastjson/1.2.51/fastjson-1.2.51.jar:/Users/feng/Developer/mvn-repository/io/netty/netty-all/4.1.33.Final/netty-all-4.1.33.Final.jar:/Users/feng/Developer/mvn-repository/org/apache/rocketmq/rocketmq-logging/4.4.0/rocketmq-logging-4.4.0.jar:/Users/feng/Developer/mvn-repository/io/netty/netty-tcnative-boringssl-static/2.0.20.Final/netty-tcnative-boringssl-static-2.0.20.Final.jar:/Users/feng/Developer/mvn-repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar io.ilss.rocketmq.demo.Main
    , label3 block, for end 
    1  if end , label3 block, for end 
    2  if end , label3 block, for end 
    3  if end , label3 block, for end 
    4 , label3 block, for end 
    5 , label3 block, for end 
    6 , for end 
    7 , for end 
    8 , for end 
    
    Process finished with exit code 0
    

    不难看出,这其实就是满足条件跳出给定label的代码块。不再执行。和单独的break其实类似,单独的break作用于循环,带label的break作用于你定义的代码块。但是有一点,break label的位置一定要在label代码块的内部。否则无效,语法错误。

    就这样咯。

    相关文章

      网友评论

          本文标题:Java基础—break label 带标签的break语句的用

          本文链接:https://www.haomeiwen.com/subject/akbupqtx.html