JDK源码阅读-------自学笔记(四)带标签的break和continues

“标签”是指后面跟一个冒号的标识符,例如:“label:”。对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。

格式:

1 label:
2         for (String string : arrayList) {
3 
4             if("d".equals(string)){
5                 stringBuilder.append(string).append(" ");
6                 break label;
7             }
8         }
View Code

源码:

 1 public String toUpperCase(Locale locale) {
 2   
 3           // 用于识别语言的标签为空,破案出空指针异常
 4            if (locale == null) {
 5               throw new NullPointerException();
 6           }
 7   
 8            int firstLower;
 9     
10            //获取数组长度
11            final int len = value.length;
12   
13          /* Now check if there are any characters that need to be changed. */
14            scan:
15           {
16                for (firstLower = 0; firstLower < len; ) {
17                    int c = (int) value[firstLower];
18                    int srcCount;
19                   if ((c >= Character.MIN_HIGH_SURROGATE)
20                          && (c <= Character.MAX_HIGH_SURROGATE)) {
21                       c = codePointAt(firstLower);
22                       srcCount = Character.charCount(c);
23                    } else {
24                       srcCount = 1;
25                  }
26                   int upperCaseChar = Character.toUpperCaseEx(c);
27                   if ((upperCaseChar == Character.ERROR)
28                           || (c != upperCaseChar)) {
29                        break scan;
30                  }
31                    firstLower += srcCount;
32               }
33               return this;
34          }
View Code

应用场景:

      想从内部循环跳到外部循环.

原文地址:https://www.cnblogs.com/liuyangfirst/p/12354655.html