读书人

一个字符串www+=空值的null后 为啥就表

发布时间: 2011-12-24 23:03:24 作者: rapoo

一个字符串www+=空值的null后 为什么就表成wwwnull了
有人知道一个字符串
www+=空值的null后 为什么就表成wwwnull了
String s="www";

s+=null;

S最后的值是wwwnull

[解决办法]
因为这里把null当成字符串编译了吧
[解决办法]
null是所有类的未实例化的一个对象,所以他可以转型成由低向上的转型。

如果转型的几个类是有上而下继承下来,null转成最低层的那个类。


[解决办法]
String Initializes

Java code
    public String() {    this.offset = 0;    this.count = 0;    this.value = new char[0];    }    public String(String original) {    int size = original.count;    char[] originalValue = original.value;    char[] v;      if (originalValue.length > size) {         // The array representing the String is bigger than the new         // String itself.  Perhaps this constructor is being called         // in order to trim the baggage, so make a copy of the array.            int off = original.offset;            v = Arrays.copyOfRange(originalValue, off, off+size);     } else {         // The array representing the String is the same         // size as the String, so no point in making a copy.        v = originalValue;     }    this.offset = 0;    this.count = size;    this.value = v;    } 

读书人网 >J2SE开发

热点推荐