我想澄清一下我是否理解正确:
== - >是参考比较,即两个对象都指向相同的内存位置.equals() - >计算对象中值的比较我的理解是正确的吗?
为什么以下工作?我希望有人会NullPointerException被扔掉.
String s = null;
s = s + "hello";
System.out.println(s); // prints "nullhello"
Run Code Online (Sandbox Code Playgroud) 我有以下代码
System.out.println("" + null);
Run Code Online (Sandbox Code Playgroud)
而输出是null.
Java如何在字符串连接中发挥作用?
我正在检查String.valueOf方法,发现null传递给valueOf它时返回"null"字符串而不是纯java null.
我的问题是为什么有人会返回"null"字符串为什么不是纯java null.
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Run Code Online (Sandbox Code Playgroud)
我问这个是因为开发人员必须检查字符串的equals方法"null"而不是stringObj != null.
[更新]:好的,这个问题有投票,我不是在api提出问题,但在我现在的公司有很多api返回"null"字符串而不是纯java null,这就是为什么我问这个问题,知道是否是它"null"如果找到null字符串对象,则返回字符串的最佳做法.