读书人

为什么要声明抛出nullpointexception和

发布时间: 2011-12-28 22:45:21 作者: rapoo

为什么要声明抛出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);    } 


这儿方法为什么要用同步,用同步有什么好处,怎么就偏偏要在stringbuffer里面用同步呢?string就没有用。

[解决办法]
有属性,但是不会出现计算异常。如果只是一个String name的属性,只会出现后面的赋值覆盖了前面的赋值,但是不会出现类似这样的代码:

Java code
     static StringBuilder buff = new StringBuilder(1);    public static void main(String[] args) throws Exception {        new Thread() {            public void run() {                buff.append("XXXXX");            }        }.start();                buff.append("CC");        System.out.println(buff);        } 

读书人网 >J2SE开发

热点推荐