Java关于scan,break scan
看源码的时候,看到了新的语法,scan,break scan。
下面源码摘抄于String.class的toUpperCase(Locale locale)方法
/* Now check if there are any characters that need to be changed. */ scan: { for (firstLower = 0 ; firstLower < len; ) { int c = (int)value[firstLower]; int srcCount; if ((c >= Character.MIN_HIGH_SURROGATE) && (c <= Character.MAX_HIGH_SURROGATE)) { c = codePointAt(firstLower); srcCount = Character.charCount(c); } else { srcCount = 1; } int upperCaseChar = Character.toUpperCaseEx(c); if ((upperCaseChar == Character.ERROR) || (c != upperCaseChar)) { break scan; } firstLower += srcCount; } return this; }
这语法是用来跳出多重循环的,'scan' 是自定义的,例子如下:
System.out.println("START"); a: { for (int i = 0; i < 10; i++ ) { System.out.println("START-" + i + "---------------"); for (int j = 0; j < 10; j++) { System.out.println(j); if (j == 3) { break a; } } System.out.println("END-" + i + "---------------"); } } System.out.println("END");
输出如下:
START START-0--------------- 0 1 2 3 END
可以尝试把break a;改成break;看看输出结果。