当前位置:   article > 正文

String中null变为"null"字符串的问题_将string中的null转为“”

将string中的null转为“”

====================================================================================

代码:

  1. String str1 = null;
  2. String str2 = "test";
  3. str1 = str1 + str2;
  4. System.out.println(str1);

输出结果:nulltest

这里它把null解析为字符串了。


原因:

我们在进行字符连接操作时会默认去调用String的valueOf()方法。

  1. /**
  2. * Returns the string representation of the <code>Object</code> argument.
  3. *
  4. * @param obj an <code>Object</code>.
  5. * @return if the argument is <code>null</code>, then a string equal to
  6. * <code>"null"</code>; otherwise, the value of
  7. * <code>obj.toString()</code> is returned.
  8. * @see java.lang.Object#toString()
  9. */
  10. public static String valueOf(Object obj) {
  11. return (obj == null) ? "null" : obj.toString();
  12. }

具体我们可以debug时查看。

至于:如果对JDK的源代码进行调试可参照本人的”Eclipse的相关使用“下Eclipse调试进入JDK源码(http://blog.csdn.net/yjtgod/article/details/9798793)文章。







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/320839
推荐阅读
相关标签
  

闽ICP备14008679号