直接使用
这是最直接、最底层的方法,在 Java 中,null 是一个特殊的字面量,表示“没有对象引用”,你可以直接使用 操作符来检查一个引用变量是否指向 null。

String str = null;
// 直接判断
if (str == null) {
System.out.println("字符串 str 是 null");
} else {
System.out.println("字符串 str 不是 null");
}
// 另一个例子
String anotherStr = "hello";
if (anotherStr == null) {
System.out.println("字符串 anotherStr 是 null");
} else {
System.out.println("字符串 anotherStr 不是 null"); // 这行会被执行
}
关键点:
- 比较的是引用地址,对于
null,它检查的是变量是否没有指向任何内存地址。 - 永远不要用 来比较字符串的内容,应该用
.equals()方法。"abc" == new String("abc")的结果是false,因为它们是两个不同的对象实例。
更常见的场景:判断 null 或空字符串
在实际开发中,我们通常关心的不仅仅是 null,还包括字符串是否为空(即长度为0的 ),这时就需要组合判断。
组合使用 和 .length()
这是最经典、最高效的方式之一。
String str = null;
// String str = "";
// String str = "hello";
if (str == null || str.length() == 0) {
System.out.println("字符串 str 是 null 或空字符串");
} else {
System.out.println("字符串 str 不是 null 也不是空字符串");
}
逻辑解析:

str == null:首先检查str是否为null,如果是,整个if条件为真(短路效应,后面的str.length()不会被执行,避免了NullPointerException)。str.length() == 0:str不是null,再检查它的长度是否为0,如果是,条件也为真。
组合使用 和 .isEmpty()
Java 6 引入了 isEmpty() 方法,它在功能上等同于 length() == 0,但语义更清晰。
String str = null;
// String str = "";
// String str = "hello";
if (str == null || str.isEmpty()) {
System.out.println("字符串 str 是 null 或空字符串");
} else {
System.out.println("字符串 str 不是 null 也不是空字符串");
}
推荐使用 isEmpty(),因为它更具可读性,明确表达了“检查是否为空”的意图。
最佳实践:使用 StringUtils (来自 Apache Commons Lang 或 Spring)
在大型项目中,频繁地写 if (str == null || str.isEmpty()) 会显得有些冗余,许多流行的工具库都提供了更简洁、更安全的方法。
Apache Commons Lang
这是最常用的工具库之一,你需要先添加 Maven 依赖:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version> <!-- 使用最新版本 -->
</dependency>
然后使用 StringUtils 类:
import org.apache.commons.lang3.StringUtils;
String str = null;
// String str = "";
// String str = "hello";
// isBlank() 更严格,会认为 " " (只有空格) 也是空的
if (StringUtils.isBlank(str)) {
System.out.println("字符串 str 是 null、空字符串或仅包含空白字符");
}
// isEmpty() 只判断 null 或 ""
if (StringUtils.isEmpty(str)) {
System.out.println("字符串 str 是 null 或空字符串");
}
StringUtils 常用方法对比:
| 方法 | 判断条件 | 示例 |
|---|---|---|
StringUtils.isEmpty(String str) |
null 或 (长度为0) |
StringUtils.isEmpty(null) -> true StringUtils.isEmpty("") -> true StringUtils.isEmpty(" ") -> false |
StringUtils.isNotEmpty(String str) |
!isEmpty(str) |
StringUtils.isNotEmpty("abc") -> true |
StringUtils.isBlank(String str) |
null, , 或 , "\t", "\n" 等 |
StringUtils.isBlank(" ") -> true |
StringUtils.isNotBlank(String str) |
!isBlank(str) |
StringUtils.isNotBlank(" a") -> true |
推荐: StringUtils.isBlank() 是一个非常实用的方法,因为它能处理用户输入时常见的“全是空格”的情况。
Spring Framework
如果你的项目已经使用了 Spring,可以直接使用 org.springframework.util.StringUtils。
import org.springframework.util.StringUtils;
String str = null;
// String str = "";
// String str = " ";
// hasText() 等同于 Apache Commons Lang 的 isBlank()
if (!StringUtils.hasText(str)) {
System.out.println("字符串 str 是 null、空字符串或仅包含空白字符");
}
// hasLength() 等同于 Apache Commons Lang 的 isEmpty()
if (!StringUtils.hasLength(str)) {
System.out.println("字符串 str 是 null 或空字符串");
}
Java 8+ 的 Optional (函数式风格)
Java 8 引入了 Optional,它是一个容器对象,可以包含或不包含非 null 值,它鼓励你以更函数式、更安全的方式处理可能为 null 的值。
import java.util.Optional;
String str = null;
// String str = "hello";
// ofNullable() 会创建一个 Optional 对象,str 为 null,则创建一个空的 Optional
Optional<String> optionalStr = Optional.ofNullable(str);
// 如果值存在,则执行消费操作
optionalStr.ifPresent(value -> {
System.out.println("字符串的值是: " + value);
});
// 如果值不存在,则执行 orElse 提供的默认值
String result = optionalStr.orElse("默认值");
System.out.println("处理后的结果是: " + result); // 输出: 处理后的结果是: 默认值
// 更复杂的链式调用
String processedStr = Optional.ofNullable(str)
.map(String::trim) // 如果非null,则执行trim
.filter(s -> !s.isEmpty()) // 如果trim后非空,则保留
.orElse("default"); // 否则,使用默认值
System.out.println("处理后的结果是: " + processedStr); // 输出: 处理后的结果是: default
优点:
- 显式地将可能为
null的值包装起来,使代码意图更清晰。 - 强制开发者处理“值不存在”的情况,减少
NullPointerException的风险。 - 支持函数式编程风格的链式调用,代码更简洁。
缺点:
- 对于简单的
null判断,可能会显得有些“重量级”。 - 过度使用
Optional作为方法返回类型或集合元素类型,可能会被一些开发者认为是“反模式”。
总结与推荐
| 场景 | 推荐方法 | 优点 |
|---|---|---|
简单判断是否为 null |
if (str == null) |
最基础,性能最高。 |
判断 null 或空字符串 |
if (str == null || str.isEmpty()) |
标准做法,清晰高效。 |
判断 null、 或纯空白字符串 |
StringUtils.isBlank(str) (Apache/Spring) |
功能强大,处理全面,代码简洁。(最推荐用于业务逻辑) |
| 需要处理默认值或链式操作 | Optional.ofNullable(str).orElse(...) |
函数式风格,安全,可读性好。 |
最终建议:
- 对于新项目或已有依赖:强烈推荐使用
StringUtils.isBlank(str),它简洁、安全,并且能处理绝大多数关于字符串“空”的定义。 - 对于没有外部依赖的简单项目或学习示例:使用
if (str == null || str.isEmpty())是完全正确和可接受的。 - 对于复杂的函数式处理流程:考虑使用
Optional,它能让你写出更优雅、更健壮的代码。
