名字 | 区别 |
---|---|
null | 未分配内存空间,无值,空指针 |
isEmpty() | 分配了内存空间,无值 |
"" | 分配了内存空间,有值,值为空字符串 |
判断对象是否为空:
str == null
判断值是否为空:
str.isEmpty()
public class Test {
public void test() {
String a = null;
String b = new String();
String c = "";
if (a == null) {
System.out.println("a == null : true");
} else {
System.out.println("a == null : false");
}
if (b == null) {
System.out.println("b == null : true");
} else {
System.out.println("b == null : false");
}
if (c == null) {
System.out.println("c == null : true");
} else {
System.out.println("c == null : false");
}
if (b.isEmpty()) {
System.out.println("b.isEmpty() : true");
} else {
System.out.println("b.isEmpty() : false");
}
if (c.isEmpty()) {
System.out.println("c.isEmpty() : true");
} else {
System.out.println("c.isEmpty() : false");
}
if (b.equals("")) {
System.out.println("b.equals(\"\") : true");
} else {
System.out.println("b.equals(\"\") : false");
}
if (c.equals("")) {
System.out.println("c.equals(\"\") : true");
} else {
System.out.println("c.equals(\"\") : false");
}
if (b == "") {
System.out.println("b == \"\" : true");
} else {
System.out.println("b == \"\" : false");
}
if (c == "") {
System.out.println("c == \"\" : true");
} else {
System.out.println("c == \"\" : false");
}
}
}
输出
a == null : true
b == null : false
c == null : false
b.isEmpty() : true
c.isEmpty() : true
b.equals("") : true
c.equals("") : true
b == "" : false
c == "" : true
网友评论