Java日常开发之判空详解

1.1 字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串""null的区别

1.1.1 ""null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null
  • "" 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null
String 并不是基本数据类型,而是对象所以会被默认的赋予 null

1.1.2 isEmpty()isBlank()

isEmpty() isBlank() 区别在于isBlank() 可以多了对于空格的判断,可以根据方法名区别使用 isEmpty() 判断字符串是否为空,而isBlank()则是判断字符串是否是空格,空或null

isEmpty(String str)

isEmpty()isNotEmpty()判段字符串是否为空或者null空格返回false

/**
 * <p>Checks if a String is empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 * StringUtils.isEmpty("bob")     = false
 * StringUtils.isEmpty("  bob  ") = false
 * </pre>
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer trims the String.
 * That functionality is available in isBlank().</p>
 *
 * @param str  the String to check, may be null
 * @return <code>true</code> if the String is empty or null
 */
public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
}

isBlank(String str)

isBlank(), isNotBlank() 判段字符串是否为空或者null,空格返回true

/**
 * <p>Checks if a String is whitespace, empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 */
public static boolean isBlank(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

上文讨论的StringUtils属于package org.apache.commons.lang;

1.2 字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串"" null 的区别

1.2.1 "" null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • "" 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null
String 并不是基本数据类型,而是对象所以会被默认的赋予 null

isEmpty() isBlank()区别在于isBlank()可以多了对于空格的判断,可以根据方法名区别使用isEmpty()判断字符串是否为空,而isBlank()则是判断字符串是否是空格,空或null

isEmpty(String str)
isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回false

/**
 * <p>Checks if a String is empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 * StringUtils.isEmpty("bob")     = false
 * StringUtils.isEmpty("  bob  ") = false
 * </pre>
 */
public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
}

isBlank(String str)
isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回true

/**
 * <p>Checks if a String is whitespace, empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 */
public static boolean isBlank(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

上文讨论的 CollectionUtils 属于package org.apache.commons.collections;

1.3 集合判空检查

要了解集合判空方法的区别首先要理解对象为size == 0null 的区别

1.3.1 size==0null 的区别

  • null是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null
  • size==0 表示集合已经指向一个地址,但是指向的对象中没有元素。

isEmpty(Collection coll)

isEmpty(), isNotEmpty() 判段集合是否为null或者不包含任何元素,null 返回true

public static boolean isEmpty(Collection coll) {
    return (coll == null || coll.isEmpty());
}

1.4 总结

1.4.1 StringUtils

StringUtils工具类是用来做字符串的操作的,小菜知道的有两种,org.apache.commons.lang3包下的StringUtils类和org.springframework.util包下的StringUtils类,org.apache.commons.lang3包下的StringUtils类常用的判空有两种:isEmptyisBlank。下面分别介绍一下。

1.org.apache.commons.lang3和org.springframework.util包中的StringUtils类的区别:
  org.apache.commons.lang3包下的StringUtils类,判断是否为空的方法参数是字符序列类,也就是string类型

org.springframework.util包下的参数是object类,也就是不仅能判断string类型,还能判断其他类型,如long等类型(此包下的更实用)

2.isEmptyisBlank的区别:
  这两个方法都是判断字符是否为空的,前者要求没有任何字符,后者要求是空白字符,isBank判断的空字符其实包括了isEmpty,也就是说isEmpty判断的范围比isBank小。而两者最大的区别就在于——isBank能判空格符而isEmpty不能。

1.4.2 CollectionUtils

CollectionUtilsorg.apache.commons.collections包中的工具类,提供了很多对集合的操作方法。

//(1)判空
CollectionUtils.isNotEmpty(resultApply)
//(2)并集
CollectionUtils.union(listA,listB);
//(3)交集
CollectionUtils.intersection(listA,listB);
//(4)补集
CollectionUtils.disjunction(listA,listB);
//(5)差集
CollectionUtils.subtract(listA,listB);
//(6)是否相等
CollectionUtils.isEqualCollection(listA,listB);
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunnyday0426

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值