前期测试代码
package com.study.string;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;
/**
* @ClassName StringTest
* @Description Java中如何判null或""
* @Author Jiangnan Cui
* @Date 2023/3/21 21:54
* @Version 1.0
*/
public class StringTest {
public static void main(String[] args) {
String string = "test";
String nullString = null;
String emptyString = "";
// 0.判null + 判"":直观、方便,但效率极低
if(nullString == null || nullString == ""){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString == ""){// 我是 !
System.out.println("我是 !");
}
// 1.判null + 判"":判""前要保证字符串不为null,否则调用String方法时会抛出空指针异常
if(nullString == null || nullString.length() == 0){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString.length() == 0){// 我是 !
System.out.println("我是 !");
}
// 2.判null + 判"":判""前要保证字符串不为null,且要将非null的字符串放在前面,否则会产生空指针异常
if(nullString == null || "".equals(nullString)){//我是null!
System.out.println("我是null!");
}
if(emptyString == null || "".equals(emptyString)){// 我是 !
System.out.println("我是 !");
}
// 3.判null + 判"":判""前要保证字符串不为null,否则调用String方法时会抛出空指针异常
if(nullString == null || nullString.isEmpty()){// 我是null!
System.out.println("我是null!");
}
if(emptyString == null || emptyString.isEmpty()){// 我是 !
System.out.println("我是 !");
}
/**
* 底层源码:
* private final char value[];
* public boolean isEmpty() {
* return value.length == 0;
* }
* 底层还是调用数组的length方法,前提是不为null
*/
// 4.判null + 判""
if (StringUtils.isEmpty(nullString)) {// 我是null!
System.out.println("我是null!");
}
if (StringUtils.isEmpty(emptyString)) {// 我是 !
System.out.println("我是 !");
}
/**
* 底层源码:
* @Deprecated
* public static boolean isEmpty(@Nullable Object str) {
* return str == null || "".equals(str);
* }
* 底层是分别判断字符串是否为null或者为“”
*/
// 5.Hutool中的isEmpty方法:判null + 判""
boolean b = StrUtil.isEmpty(string);// b = false
System.out.println("b = " + b);
boolean isNull = StrUtil.isEmpty(nullString);// isNull = true
System.out.println("isNull = " + isNull);
boolean isEmpty = StrUtil.isEmpty(emptyString);// isEmpty = true
System.out.println("isEmpty = " + isEmpty);
/**
* 底层源码:
* public static boolean isEmpty(CharSequence str) {
* return str == null || str.length() == 0;
* }
* 底层是分别判断字符串是否为null或者为“”
*/
// 6.Hutool中的isBlank方法:判null + 判""
b = StrUtil.isBlank(string);// b = false
System.out.println("b = " + b);
isNull = StrUtil.isBlank(nullString);// isNull = true
System.out.println("isNull = " + isNull);
isEmpty = StrUtil.isBlank(emptyString);// isEmpty = true
System.out.println("isEmpty = " + isEmpty);
/**
* 底层源码:
* public static boolean isBlank(CharSequence str) {
* int length;
* if (str != null && (length = str.length()) != 0) {
* for(int i = 0; i < length; ++i) {
* if (!CharUtil.isBlankChar(str.charAt(i))) {
* return false;
* }
* }
* return true;
* } else {
* return true;
* }
* }
*/
/**
* 推荐使用Hutool工具类的isEmpty或isBlank方法:
*/
System.out.println(StrUtil.isEmpty("test"));// false
System.out.println(StrUtil.isEmpty(""));// true
System.out.println(StrUtil.isEmpty(null));// true
System.out.println(StrUtil.isBlank("test"));// false
System.out.println(StrUtil.isBlank(""));// true
System.out.println(StrUtil.isBlank(null));// true
System.out.println(StrUtil.isNotEmpty("test"));// true
System.out.println(StrUtil.isNotEmpty(""));// false
System.out.println(StrUtil.isNotEmpty(null));// false
System.out.println(StrUtil.isNotBlank("test"));// true
System.out.println(StrUtil.isNotBlank(""));// false
System.out.println(StrUtil.isNotBlank(null));// false
}
}
总结
实际应用中推荐使用Hutool工具类中的StrUtil类下方法(isEmpty、isNotEmpty、isBlank、isNotBlank)进行判null或者判"",对应源码如下:
public static boolean isEmptyIfStr(Object obj) {
if (null == obj) {
return true;
} else if (obj instanceof CharSequence) {
return 0 == ((CharSequence)obj).length();
} else {
return false;
}
}
public static boolean isNotEmpty(CharSequence str) {
return !isEmpty(str);
}
public static boolean isBlank(CharSequence str) {
int length;
if (str != null && (length = str.length()) != 0) {
for(int i = 0; i < length; ++i) {
if (!CharUtil.isBlankChar(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
public static boolean isNotBlank(CharSequence str) {
return !isBlank(str);
}