当前位置: 代码迷 >> J2SE >> 一个字符串www+=空值的null后 为啥就表成wwwnull了
  详细解决方案

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

热度:102   发布时间:2016-04-24 13:07:32.0
一个字符串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;    }
  相关解决方案