JAVA中isEmpty和null以及""的區別

這是一個比較容易混淆的概念,爲了弄清楚這個問題,最好的方法當然是寫程序來驗證,上代碼吧~~:    


/**
*
*/
package JavaTest;
/**
* @author wxwevenpc
* @version 1.0 2012-11-25
*/
public class TestNull {
/**
* @param args
*/
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 = ''");
}
}
}

以上輸出:

String a = new String
String b = ""
String c =null

分析:

此時a是分配了內存空間,但值爲空,是絕對的空,是一種有值(值存在爲空而已)
此時b是分配了內存空間,值爲空字符串,是相對的空,是一種有值(值存在爲空字串)
此時c是未分配內存空間,無值,是一種無值(值不存在)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章