在 Java 中,判断一个字符串是否为 null 是一个非常基础且常见的操作,下面我将从最简单到最全面地介绍各种判断方法,并解释它们的区别和最佳实践。

最直接的方法:使用 运算符
这是最基本、最直接的方法,专门用来检查变量是否指向 null 对象。
String myString = null;
if (myString == null) {
System.out.println("字符串是 null");
} else {
System.out.println("字符串不是 null");
}
// 输出: 字符串是 null
关键点:
- 比较的是引用地址。
null在 Java 中是一个特殊的字面量,表示“没有对象引用”,如果一个变量的值是null,那么它就不指向任何内存地址。 - 只适用于检查
null,不要用它来检查字符串是否为空内容(),那是equals()方法的工作。
判断字符串是否为空()
如果你想判断一个字符串是否是空字符串(即长度为0),应该使用 String 类的 isEmpty() 方法。
String emptyString = "";
String nonEmptyString = "Hello";
if (emptyString.isEmpty()) {
System.out.println("字符串是空的");
}
if (!nonEmptyString.isEmpty()) {
System.out.println("字符串不是空的");
}
// 输出:
// 字符串是空的
// 字符串不是空的
最常用、最安全的组合:先检查 null,再检查 isEmpty()
在实际开发中,我们通常需要同时处理字符串为 null 和为空字符串()这两种情况,一个健壮的判断逻辑应该先检查 null,避免在 null 对象上调用方法(如 isEmpty())而导致 NullPointerException。

public void checkString(String str) {
// 推荐:先检查 null,再检查空
if (str == null || str.isEmpty()) {
System.out.println("字符串为 null 或空");
} else {
System.out.println("字符串有内容: " + str);
}
}
// 测试
checkString(null); // 输出: 字符串为 null 或空
checkString(""); // 输出: 字符串为 null 或空
checkString(" "); // 输出: 字符串有内容:
checkString("Hello"); // 输出: 字符串有内容: Hello
为什么这个顺序很重要?
因为 Java 的 (逻辑或) 具有短路特性。str == null 的结果是 true,整个表达式的结果就已经是 true 了,后面的 str.isEmpty() 根本不会被评估,从而避免了 NullPointerException。
判断字符串是否为空白(包含空格、制表符等)
一个字符串可能不是 null,也不是 ,但只包含空白字符(如空格 、制表符 '\t'、换行符 '\n' 等),在这种情况下,isEmpty() 会返回 false,但你可能希望将其视为“无效”或“空白”字符串。
从 Java 11 开始,String 类提供了 isBlank() 方法来专门处理这种情况。
String blankString = " \t \n ";
String nonBlankString = " a ";
// Java 11+
if (blankString.isBlank()) {
System.out.println("字符串是空白的(只包含空白字符)");
}
if (!nonBlankString.isBlank()) {
System.out.println("字符串不是空白的");
}
// 输出:
// 字符串是空白的(只包含空白字符)
// 字符串不是空白的
最佳实践(Java 11 及以上):
将 isBlank() 与 null 检查结合使用,是目前最全面和推荐的方式。

public void checkStringComprehensively(String str) {
if (str == null || str.isBlank()) {
System.out.println("字符串为 null、空或仅包含空白字符");
} else {
System.out.println("字符串有有效内容: " + str);
}
}
// 测试
checkStringComprehensively(null); // 输出: 字符串为 null、空或仅包含空白字符
checkStringComprehensively(""); // 输出: 字符串为 null、空或仅包含空白字符
checkStringComprehensively(" \t"); // 输出: 字符串为 null、空或仅包含空白字符
checkStringComprehensively(" Hello ");// 输出: 字符串有有效内容: Hello
使用 Apache Commons Lang 或 Guava 库
在大型项目中,为了代码的简洁和一致性,开发者常常使用第三方工具库。
使用 Apache Commons Lang (StringUtils)
StringUtils 提供了非常方便的工具方法。
import org.apache.commons.lang3.StringUtils;
String str = null;
// 1. 检查是否为 null 或空
if (StringUtils.isEmpty(str)) {
System.out.println("字符串为 null 或空"); // true
}
// 2. 检查是否为 null、空或空白
if (StringUtils.isBlank(str)) {
System.out.println("字符串为 null、空或空白"); // true
}
// 同样适用于 ""
StringUtils.isEmpty(""); // true
StringUtils.isBlank(""); // true
// 适用于 " "
StringUtils.isEmpty(" "); // false
StringUtils.isBlank(" "); // true
使用 Google Guava (Strings)
Guava 也提供了类似的工具类。
import com.google.common.base.Strings;
String str = null;
// 1. 检查是否为 null 或空
if (Strings.isNullOrEmpty(str)) {
System.out.println("字符串为 null 或空"); // true
}
// 2. 检查是否为 null、空或空白
// Guava 没有直接叫 isBlank 的,但可以用 isNullOrEmpty 加上 trimToEmpty
if (Strings.isNullOrEmpty(str) || Strings.isNullOrEmpty(str.trim())) {
// 注意:str 是 null, str.trim() 会抛出 NPE
// 所以正确的写法是先处理 null
if (str == null || Strings.isNullOrEmpty(str.trim())) {
System.out.println("字符串为 null、空或空白");
}
}
// Guava 的处理方式不如 Commons Lang 的 isBlank 直接。
// 对于空白检查,Apache Commons Lang 更方便。
总结与最佳实践
| 场景 | 推荐方法 | 说明 |
|---|---|---|
仅检查是否为 null |
if (str == null) |
最基础、最高效的 null 检查。 |
| 仅检查是否为空字符串 | if (str.isEmpty()) |
专门检查长度为0的字符串。 |
检查是否为 null 或 |
if (str == null || str.isEmpty()) |
最常用、最经典的组合,安全可靠。 |
检查是否为 null、 或空白 |
if (str == null || str.isBlank()) |
Java 11+ 最佳实践,全面处理各种无效情况。 |
| 在项目中统一处理 | 使用 StringUtils.isEmpty() 或 StringUtils.isBlank() |
第三方库推荐,代码更简洁,团队约定一致。 |
最终建议:
- 如果你使用 Java 11 或更高版本,请优先使用
if (str == null || str.isBlank()),这是最现代、最全面的方法。 - 如果你使用 Java 8 或更早版本,请使用
if (str == null || str.isEmpty())。 - 如果你的项目已经引入了 Apache Commons Lang,并且团队有使用习惯,
StringUtils.isBlank()是一个非常好的选择,它代码更短,意图更清晰。
