多种方式判断字符串是否为空,效率比拼

本文对比分析了五种常见的字符串判空方法,并通过实验证明了不同场景下各种方法的性能差异。对于小规模判断推荐直接比较,大规模则建议使用StringUtils工具类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、情景
  • 判断输入字符串是否为空  
  • 分析:null   && “” || length()==0
二、区分null 与“”
  • null:字符串不指向任何东西,即null不是个对象,null没有分配空间,,未实例化,所以这时候调用它会报空指针异常
  • “” :它指向一个长度为0的字符串,即“”是个对象,“”分配了空间;已经实例化
三、应用场景
  • 正确写法if(str==null||str.equals(""))    ==>先判断字符串是否为对象,在判断是否为空字符串
  • 错误写法if(str.equals("")||str==null)    ==>这种写法容易造成空指针异常!!
  • 所以判断字符串是否为空,首先确保它不是NULL,即是个对象,然后再判断它的长度是否为0
    • if(str!=null && str.length!=0)
    • if( StringUtils.isNotBlank(str) ) ==>使用StringUtils中封装的方法StringUtils.isNotBlank()




一、实践
说明:以下五种方法,都是测试相同字符串“This is the test data”,测试循环次数:10000 次;
当测试数据足够长,次数足够多,差别才会特别明显。反复试验多次,最终得出以下结论
方法
代码
示范:测试耗时
效率
名次
备注
str == null || "".equals(str)
694ms

4

str == null || str.length() <= 0
458ms
较高
2

str == null || str.isEmpty()
601ms
次之
3
javaSE6.0之开始提供该方法;才兼容
str == null || str == ""
178ms
最高
1
 
StringUtils.isBlank(str)
744ms
最低
5
效率最低,StringUtils中封装的
总备注
当判断的循环的次数较少时,推荐方法四





二、实践
说明:以下五种方法,都是测试相同字符串“This is the test data”,测试循环次数:100000 次;
当测试数据足够长,次数足够多,差别才会特别明显。反复试验多次,最终得出以下结论
方法
代码
示范:测试耗时
效率
名次
备注
str == null || "".equals(str)
4803ms
最低
5

str == null || str.length() <= 0
2979ms

2

str == null || str.isEmpty()
2965ms
次之
3
javaSE6.0之开始提供该方法;才兼容
str == null || str == ""
2978ms
较高
2

StringUtils.isBlank(str)
2962ms
最高
1
StringUtils中封装的
总备注
当判断的循环的次数较大时,推荐方法五




public class StringJudgeController {


    public static void main(String[] args) {
        // 当测试的次数从10000  变为100000.结果差异很大!!!
        JudgeString1("This is the test data", 10000);
        JudgeString2("This is the test data", 10000);
        JudgeString3("This is the test data", 10000);
        JudgeString4("This is the test data", 10000);
        JudgeString5("This is the test data", 10000);
    }

    /**
     * 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低;
     * 方法二: 比较字符串长度, 效率高, 是最好的一个方法;
     * 方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二;
     * 方法四: 这是一种比较直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多;
     * 方法五: 这是java自己封装的方法,方便调用;但是效率最低
     */
    public static void JudgeString1(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || "".equals(str)) {

                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function1耗时:" + (endTime - startTiem) + "ms");
    }

    public static void JudgeString2(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str.length() <= 0) {

                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function2耗时:" + (endTime - startTiem) + "ms");
    }

    public static void JudgeString3(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str.isEmpty()) {

                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function3耗时:" + (endTime - startTiem) + "ms");
    }

    public static void JudgeString4(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (str == null || str == "") {

                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function4耗时:" + (endTime - startTiem) + "ms");
    }

    public static void JudgeString5(String str, long num) {
        long startTiem = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if (StringUtils.isBlank(str)) {

                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function5耗时:" + (endTime - startTiem) + "ms");
    }
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值