在翻看String源码的时候发现了以下代码:
/ *
*
* @param locale use the case transformation rules for this locale
* @return the {@code String}, converted to lowercase.
* @see java.lang.String#toLowerCase()
* @see java.lang.String#toUpperCase()
* @see java.lang.String#toUpperCase(Locale)
* @since 1.1
*/
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
中间这一部分代码:用到了Java标记 这里的scan就是一个跳转标记 通常在包含循环体的代码中 设定跳转标记 在continue或者break后添加标记 跳转到标记处开始执行 这样做的目的是跳过本次循环或跳出循环体 这里是当
- if (c != Character.toLowerCase(c))
满足时候结束for循环
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0; /* result may grow, so i+resultOffset
* is the write location in result */
/* Just copy the first few lowerCase characters. */
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent ||
srcChar == '\u03A3' || // GREEK CAPITAL LETTER SIGMA
srcChar == '\u0130') { // LATIN CAPITAL LETTER I WITH DOT ABOVE
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} else if (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* Grow result if needed */
int mapLen = lowerCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
- 小试牛刀:
/**
* 这个例子主要介绍的是java中跳转标记的使用
*
* 起因是在String.toLowercase(Locale locale)中 发现一个scan:标记
*
* @author Administrator
*
*/
public class ScanFlag {
/**
* 计算[0-length]区间内奇数的和
*
* @param length
* @return
*/
public static int computeOdds(int length) {
int sum = 0;
odd: for (int i = 0; i <=length; i++) {
if (i % 2 == 0)
continue odd;
sum += i;
}
return sum;
}
/**
* 计算[0-length]区间内偶数的和
*
* @param length
* @return
*/
public static int computeEvens(int length) {
int sum = 0;
even: for (int i = 0; i <=length; i++) {
if (i % 2 != 0)
continue even;
sum += i;
}
return sum;
}
public static void main(String[] args) {
int odds=computeOdds(10);
int evens=computeEvens(10);
System.out.println("10以内偶数的和是:"+odds);
System.out.println("10以内奇数的和是:"+evens);
}
}
执行结果如下:
10以内偶数的和是:25
10以内奇数的和是:30
网友评论