isEmpty()

/*******************************************************************************
 * @(#)JavaNull.java 2017年4月5日
 *
 * Copyright 2017 emrubik Group Ltd. All rights reserved.
 * EMRubik PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/
package test;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO 这里请补充该类型的简述说明
 * @author <a href="mailto:yuml@emrubik.com">yuml</a>
 * @version $Revision 1.0 $ 2017年4月5日 上午10:14:25
 */
public class JavaNull {
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String a = new String();  
        String b = "";  
        String c = null; 
        List<String> list = new ArrayList<String>();
        if(a.isEmpty()) {  
            System.out.println("String a = new String");  
        }  
        if(b.isEmpty()) {  
            System.out.println("String b = """);  
        }
        /*if(c.isEmpty()) {  
            System.out.println("String c =null");  
        }*/
        if(c == null) {  
            System.out.println("String c =null");  
        }
        if(list.isEmpty()) {  
            System.out.println("list");  
        }
        if(null == a) {  
            System.out.println("String a = null");  
        }  
        if(a == "") {  
            System.out.println("a = ''");  
        }  
    }  
}

  


以上输出:

[java] view plain copy
 
 print?
  1. String a = new String  
  2. String b = ""  
  3. String c =null  


分析:

[java] view plain copy
 
 print?
    1. 此时a是分配了内存空间,但值为空,是绝对的空,是一种有值(值存在为空而已)  
    2. 此时b是分配了内存空间,值为空字符串,是相对的空,是一种有值(值存在为空字串)  
    3. 此时c是未分配内存空间,无值,是一种无值(值不存在)
原文地址:https://www.cnblogs.com/pureEve/p/6667616.html