在Java中如何检查字符串是否为空

来源:AI教程网作者:小菜鸟头衔:草根站长
导读:本期聚焦于小伙伴创作的《在Java中如何检查字符串是否为空》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《在Java中如何检查字符串是否为空》有用,将其分享出去将是对创作者最好的鼓励。

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

在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,使用更方便。常用的有isEmptyisBlank两个方法。

使用前需要引入依赖,如果是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包下,常用的判空方法是hasLengthhasText

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

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。