public class HelloWorld {
public static void main(String args[]) {
String s = null;
s = s+"word";
System.out.println("hello " +s);
}
}

试问:输出结果?

  hello nullword

解释:

s = s+"word"; 等价于 s = String.valueOf(s)+"word";  Integer,Double都一样。

String的源码:

public static String valueOf(Object obj) {
  return (obj == null) ? "null" : obj.toString();
}


相关文章:

  • 2021-07-26
  • 2021-06-29
  • 2021-09-14
  • 2021-11-26
  • 2022-12-23
  • 2021-07-03
  • 2021-07-20
猜你喜欢
  • 2021-09-10
  • 2021-06-29
  • 2021-09-24
  • 2021-08-09
  • 2021-05-16
  • 2021-12-26
  • 2021-12-29
相关资源
相似解决方案