Java中关于isEmpty方法、null以及““的区别
作者:望穿秋水见伊人
这篇文章主要介绍了Java中关于isEmpty方法、null以及““的区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
关于isEmpty方法、null以及““的区别
这是一个比较容易混淆的概念,为了弄清楚这个问题,最好的方法当然是写程序来验证,开门见山,上代码!
public class TestNull {
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 = ''");
}
if (a.equals("")) {
// 由于a是字符串,字符串的比较需要用equals,不能直接用 ==
System.out.println("a.equals(\"\") is true");
}
/*if (c.isEmpty()) {
// 这里会报空指针,即null不能使用此方法
System.out.println("c == null and c.isEmpty");
}*/
List<String> list = new ArrayList<>();
// list.add("");
if (list.isEmpty()) {
System.out.println("list is empty");
}
System.out.println(list.size());
}
}控制台输出

分析
- 此时a是分配了内存空间,但值为空,是绝对的空,是一种有值(值存在为空而已)。
- 此时b是分配了内存空间,值为空字符串,是相对的空,是一种有值(值存在为空字串)。
- 此时c是未分配内存空间,无值,是一种无值(值不存在)。
综上所述
| isEmpty() | 分配了内存空间,值为空,是绝对的空,是一种有值(值 = 空) |
| "" | 分配了内存空间,值为空字符串,是相对的空,是一种有值(值 = 空字串) |
| null | 是未分配内存空间,无值,是一种无值(值不存在) |
Java如何快速判null或““
前期测试代码
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);
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
