首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

null被挟制转化为字符

2012-08-30 
null被强制转化为字符?String strnullSystem.out.println((str+chen))输出结果为 nullchen为什么呢?

null被强制转化为字符?
String str=null;
System.out.println((str+="chen"));
输出结果为 nullchen
为什么呢?

[解决办法]

探讨
String str=null;
System.out.println((str+="chen"));
输出结果为 nullchen
为什么呢?

[解决办法]
不好意思,这个解释文不对题,没仔细看LZ问题

String str=null;
System.out.println((str+="chen"));

等价于
String str=null;
System.out.println(new StringBuilder().append(str).append("chen").toString);

我们要看的是append方法的源码
Java code
public AbstractStringBuilder append(String str) {    if (str == null) str = "null";        int len = str.length();    if (len == 0) return this;    int newCount = count + len;    if (newCount > value.length)        expandCapacity(newCount);    str.getChars(0, len, value, count);    count = newCount;    return this;    }
[解决办法]
探讨

Prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and the……

热点排行