Java String类的isEmpty(),null的区别

总结为,isEmpty()完全等同于string.length()==0
如果String对象本身是null,即字符串对象的引用是空指针,那么使用string.isEmpty()会报NullPointerException

判断一个String为空的安全方法,string == null || string.isEmpty()

例子:

//字符串对象的引用a是有效指针,字符串对象a分配内存空间,并且指向的内容为空字符串(在java没有指针的说法,但这样理解更好)
String a = new String();
//字符串对象的引用b是有效指针,字符串对象b分配内存空间,并且指向的内容为空字符串
String b = "";
//字符串对象的引用c是空指针, 字符串对象c未分配内存空间
String c = null;

isEmpty()使用的前提是字符串对象已经被分配了内存空间,如果对象没有被分配空间而使用isEmpty()报空指针错误,isEmpty等同于string.length()==0,比如对字符串对象的引用c: c.isEmpty(),报NullPointException,而字符串对象的引用a,b都不会。

null的使用,可以用来判断字符串对象的引用是否是空指针,比如c == null,结果是true,因为c的确是空指针。

public class TestString {
    public static void main(String[] args) {
        //为字符串对象a分配内存空间,并且这块空间未初始化
        String a = new String();
        //为字符串对象b分配内存空间,并且指向空字符串
        String b = "";
        //字符串对象c未分配内存空间
        String c = null;

        if (a != null) {
            System.out.println("String a = new String(); 字符串对象的引用a不是空指针,指向了某块内存区域");
        } else {
            //dead code
            System.out.println("String a = new String(); 字符串对象的引用a是空指针");
        }

        if (b != null){
            System.out.println("String b = \"\"; 字符串对象的引用b不是空指针,指向了某块内存区域");
        } else {
            //dead codeb
            System.out.println("String b = \"\"; 字符串对象的引用b是空指针");
        }

        if (c != null) {
            //dead code
            System.out.println("String c = null; 字符串对象的引用b不是空指针,指向了某块内存区域");
        } else {
            System.out.println("String c = null; 字符串对象的引用c是空指针");
        }

        if (a.equals("")) {
            //dead code
            System.out.println("String a = new String(); 字符串对象的引用a指向的某块内存区域的内容为空字符串");
        } else {
            System.out.println("String a = new String(); 字符串对象a指向了某块内存区域的内容不是空字符串");
        }

        if (b.equals("")) {
            System.out.println("String b = \"\"; 字符串对象的引用b指向的某块内存区域的内容为空字符串");
        } else {
            //dead code
            System.out.println("String b = \"\"; 字符串对象b指向了某块内存区域的内容不是空字符串");
        }

//      字符串对象c实际上未被分配空间,换句话说,c并没有指向内存区域,所以无法测试c指向内存区域的内容。
//      也就没必要测试c指向的内存区域是否为空字符串

        if (a.isEmpty()) {
            System.out.println("String a = new String(); 字符串对象a指向了某块内存区域且区域的长度是:" + a.length());
        }
        if (b.isEmpty()) {
            System.out.println("String b = \"\"; 字符串对象a指向了某块内存区域且区域的长度是:" + b.length());
        }

        if (a.equals(b) && a.equals("")) {
            System.out.println("实际上a与b的内容是相同的,都是\"\",即空字符串, 不信可以看看下面String类源码中的无参数构造函数的实现");
        }

//      附带部分String类的源码
        /** The value is used for character storage. */
//        private final char value[];

//        public boolean isEmpty() {
//            return value.length == 0;
//        }
        /** Initializes a newly created object so that it represents an empty character sequence.*/
//        public String() {
//            this.value = "".value;
//        }
     }
}

原文:https://blog.csdn.net/baidu_31945865/article/details/79864016 

 

 

自己的代码:

public class Two_2019528 {
	public static void main(String[] args) {
		/*
		 * true
		 * false
		 * false
		 * 
		String i = "";
		System.out.println(i.isEmpty());
		System.out.println(i==null);
		System.out.println(i.equals(null));*/
		/*
		 * false
		 *
		String i = " ";
		System.out.println(i.isEmpty()); */
		/**
		 * NullPointerException
		 *
		String i = null;
		System.out.println(i.isEmpty());*/
	}

}


 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值