1.比较equals
String name = getName();
if (name.equals("test")) {
System.out.println("hello");
}
如果name为空,
NullPointerException
----》推荐
String name = getName();
if ("test".equals(name)) {
System.out.println("hello");
}
equals如果name为空,直接返回false
2.转化为String
Object name = getName();
System.out.println(name.toString());
name为空会导致空指针
---推荐
Object name = getName();
System.out.println((String)name);
name为空会输出null