1
參考文檔
這是一個比較容易混淆的概念,爲了弄清楚這個問題,最好的方法當然是寫程序來驗證,開門見山,上代碼!
public class TestNull {
public static void main(String[] args) {
String a = new String();
String b = "";
String c = null;
if (a.isEmpty()) {
System.out.println("String a = new String");
}
if (b.isEmpty()) {
System.out.println("String b = \"\"");
}
if (c == null) {
System.out.println("String c =null");
}
if (null == a) {
System.out.println("String a =null");
}
if (a == "") {
System.out.println("a = ''");
}
}
}
控制檯輸出:
分析:
此時a是分配了內存空間,但值爲空,是絕對的空,是一種有值(值存在爲空而已)。
此時b是分配了內存空間,值爲空字符串,是相對的空,是一種有值(值存在爲空字串)。
此時c是未分配內存空間,無值,是一種無值(值不存在)。
綜上所述:
isEmpty() | 分配了內存空間,值爲空,是絕對的空,是一種有值(值 = 空) |
---|---|
" " | 分配了內存空間,值爲空字符串,是相對的空,是一種有值(值 = 空字串) |
null | 是未分配內存空間,無值,是一種無值(值不存在) |