在Java开发中,字符串判空是几乎每个项目都会用到的操作,错误的判空逻辑很容易引发空指针异常,影响程序稳定性。不同的判空方式适用场景不同,需要根据实际需求选择。

原生Java判空方式
检查是否为null
首先要明确,null表示字符串引用没有指向任何对象,此时调用任何字符串实例方法都会抛出空指针异常,所以判空的第一步通常是检查是否为null。
public class StringCheckDemo {
public static void main(String[] args) {
String str1 = null;
// 检查是否为null
if (str1 == null) {
System.out.println("str1是null");
}
String str2 = "test";
if (str2 != null) {
System.out.println("str2不是null");
}
}
}
使用String类的isEmpty方法
当字符串不是null时,可以使用isEmpty()方法判断字符串是否为空串(长度为0的字符串)。注意这个方法不能对null字符串调用,否则会抛异常。
public class StringCheckDemo {
public static void main(String[] args) {
String str1 = "";
String str2 = "hello";
// 先判null再调用isEmpty
if (str1 != null && str1.isEmpty()) {
System.out.println("str1是空串");
}
if (str2 != null && !str2.isEmpty()) {
System.out.println("str2不是空串");
}
}
}
使用String类的isBlank方法(Java 11及以上)
Java 11新增了isBlank()方法,它不仅会判断字符串是否为空串,还会判断字符串是否只包含空白字符(空格、制表符、换行符等),实用性更强。
public class StringCheckDemo {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = "hello";
// 先判null再调用isBlank
if (str1 != null && str1.isBlank()) {
System.out.println("str1是空白字符串");
}
if (str2 != null && str2.isBlank()) {
System.out.println("str2是空白字符串");
}
if (str3 != null && !str3.isBlank()) {
System.out.println("str3不是空白字符串");
}
}
}
第三方工具类判空方式
Apache Commons Lang的StringUtils
Apache Commons Lang库提供了StringUtils工具类,里面的判空方法已经处理了null的情况,不用手动先判null,使用更方便。常用的有isEmpty和isBlank两个方法。
使用前需要引入依赖,如果是Maven项目,添加如下依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
判空代码示例:
import org.apache.commons.lang3.StringUtils;
public class StringCheckDemo {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "hello";
// isEmpty会判断null和空串,不会判断空白字符
System.out.println(StringUtils.isEmpty(str1)); // true
System.out.println(StringUtils.isEmpty(str2)); // true
System.out.println(StringUtils.isEmpty(str3)); // false
System.out.println(StringUtils.isEmpty(str4)); // false
// isBlank会判断null、空串和空白字符
System.out.println(StringUtils.isBlank(str1)); // true
System.out.println(StringUtils.isBlank(str2)); // true
System.out.println(StringUtils.isBlank(str3)); // true
System.out.println(StringUtils.isBlank(str4)); // false
}
}
Spring Framework的StringUtils
Spring框架也提供了自己的StringUtils工具类,位于org.springframework.util包下,常用的判空方法是hasLength和hasText。
import org.springframework.util.StringUtils;
public class StringCheckDemo {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "hello";
// hasLength判断null和空串,返回false表示是null或者空串
System.out.println(StringUtils.hasLength(str1)); // false
System.out.println(StringUtils.hasLength(str2)); // false
System.out.println(StringUtils.hasLength(str3)); // true
System.out.println(StringUtils.hasLength(str4)); // true
// hasText判断null、空串、空白字符,返回false表示是null、空串或者空白字符
System.out.println(StringUtils.hasText(str1)); // false
System.out.println(StringUtils.hasText(str2)); // false
System.out.println(StringUtils.hasText(str3)); // false
System.out.println(StringUtils.hasText(str4)); // true
}
}
不同判空方式的对比
为了更直观了解不同方法的差异,整理如下对比表格:
| 方法 | 是否支持null | 是否判断空串 | 是否判断空白字符 | 适用场景 |
|---|---|---|---|---|
| str == null | 是 | 否 | 否 | 仅需要判断引用是否为空 |
| str.isEmpty() | 否 | 是 | 否 | Java原生,判断非null的空串 |
| str.isBlank() | 否 | 是 | 是 | Java 11及以上,判断非null的空白字符串 |
| StringUtils.isEmpty(str) | 是 | 是 | 否 | Apache Commons Lang,快速判断null或空串 |
| StringUtils.isBlank(str) | 是 | 是 | 是 | Apache Commons Lang,判断null、空串或空白字符 |
| StringUtils.hasLength(str) | 是 | 是 | 否 | Spring项目,判断是否有长度(非null非空串) |
| StringUtils.hasText(str) | 是 | 是 | 是 | Spring项目,判断是否有实际文本(非null、非空串、非空白) |
判空最佳实践
在实际开发中,建议遵循以下原则选择判空方式:
- 如果是简单的判空,且不需要处理空白字符,Java 8及以下可以用
str != null && !str.isEmpty()判断是否非空,Java 11及以上可以用str != null && !str.isBlank()判断是否有实际内容。 - 如果项目已经引入了Apache Commons Lang或者Spring框架,优先使用对应的工具类判空方法,减少重复代码,也避免遗漏null判断导致的异常。
- 如果业务场景中认为空白字符也是空值,优先选择
isBlank相关的方法,否则用isEmpty相关的方法即可。 - 不要在工具类的判空方法上再手动加null判断,比如写成
str != null && StringUtils.isBlank(str),这是多余的,工具类已经处理了null的情况。
注意:new String()创建的字符串不是null,调用isEmpty()会返回true,调用isBlank()也会返回true,和空串""的表现一致。
Java字符串判空isEmptyStringUtilsnull检查修改时间:2026-06-26 01:33:48