赞
踩
====================================================================================
代码:
- String str1 = null;
- String str2 = "test";
- str1 = str1 + str2;
- System.out.println(str1);
输出结果:nulltest
这里它把null解析为字符串了。
原因:
我们在进行字符连接操作时会默认去调用String的valueOf()方法。
- /**
- * Returns the string representation of the <code>Object</code> argument.
- *
- * @param obj an <code>Object</code>.
- * @return if the argument is <code>null</code>, then a string equal to
- * <code>"null"</code>; otherwise, the value of
- * <code>obj.toString()</code> is returned.
- * @see java.lang.Object#toString()
- */
- public static String valueOf(Object obj) {
- return (obj == null) ? "null" : obj.toString();
- }
具体我们可以debug时查看。
至于:如果对JDK的源代码进行调试可参照本人的”Eclipse的相关使用“下Eclipse调试进入JDK源码(http://blog.csdn.net/yjtgod/article/details/9798793)文章。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。