JAVA中isEmpty和null以及""的区别

来源:互联网 发布:jquery.format.js 编辑:程序博客网 时间:2024/05/18 03:41

转自http://blog.csdn.net/lhflower123/article/details/8223607

    /**      *       */      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是未分配内存空间,无值,是一种无值(值不存在)

原创粉丝点击