当前位置: 代码迷 >> J2SE >> 为什么要声明抛出nullpointexception和为什么要同步?解决方法
  详细解决方案

为什么要声明抛出nullpointexception和为什么要同步?解决方法

热度:100   发布时间:2016-04-24 12:41:59.0
为什么要声明抛出nullpointexception和为什么要同步?
看jdk的string类源代码:

Java code
 public String toUpperCase(Locale locale) {    if (locale == null) {//偶不明白为什么这儿要判断一下,是否抛出空指针。因为空指针在运行的时候可以由jvm捕获的呀????        throw new NullPointerException();        }        int     firstLower;    /* Now check if there are any characters that need to be changed. */    scan: {        for (firstLower = 0 ; firstLower < count; ) {        int c = (int)value[offset+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;    }        char[]  result       = new char[count]; /* may grow */    int     resultOffset = 0;  /* result may grow, so i+resultOffset                    * is the write location in result */    /* Just copy the first few upperCase characters. */    System.arraycopy(value, offset, result, 0, firstLower);    String lang = locale.getLanguage();    boolean localeDependent =            (lang == "tr" || lang == "az" || lang == "lt");        char[] upperCharArray;        int upperChar;        int srcChar;        int srcCount;        for (int i = firstLower; i < count; i += srcCount) {        srcChar = (int)value[offset+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) {                upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);            } else {                upperChar = Character.toUpperCaseEx(srcChar);            }            if ((upperChar == Character.ERROR) ||                (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {                if (upperChar == Character.ERROR) {                    if (localeDependent) {                        upperCharArray =                            ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);                    } else {                        upperCharArray = Character.toUpperCaseCharArray(srcChar);                    }                } else if (srcCount == 2) {            resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;            continue;                } else {                    upperCharArray = Character.toChars(upperChar);        }                /* Grow result if needed */                int mapLen = upperCharArray.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] = upperCharArray[x];                }                resultOffset += (mapLen - srcCount);            } else {                result[i+resultOffset] = (char)upperChar;            }        }        return new String(0, count+resultOffset, result);    }  



且看jdk的stringbuffer的源代码:
Java code
  public synchronized int lastIndexOf(String str, int fromIndex) {        return String.lastIndexOf(value, 0, count,                              str.toCharArray(), 0, str.length(), fromIndex);    }    /**     * @since   JDK1.0.2     */    public synchronized StringBuffer reverse() {    super.reverse();    return this;    }    public synchronized String toString() {    return new String(value, 0, count);    }
  相关解决方案