判断java String字符串是否为空或null

有时在用字符串获取一个方法的返回值时,预先不知道是否为空,所以需要判断一下。一般String有equals()、length()、isEmpty()方法。先来看一组代码:

public class Test {

    public static void nullPoint1() {
        String test = null;
        try {
            if (test.equals(null)) {
                System.out.println("test is null !");
            } 
        } catch (NullPointerException e) {
            System.out.println("test.equals(null): " + e);
        }
    }

    public static void nullPoint2() {
        String test = null;
        try {
            if (test.length() == 0) {
                System.out.println("test is null !");
            } 
        } catch (NullPointerException e) {
            System.out.println("test.length() == 0: " + e);
        }
    }

    public static void nullPoint3() {
        String test = null;
        try {
            if (test.isEmpty()) {
                System.out.println("test is null !");
            } 
        } catch (NullPointerException e) {
            System.out.println("test.isEmpty(): " + e);
        }
    }

    public static void nullPoint4() {
        String test = null;
        try {
            if (test.equals("")) {
                System.out.println("test is null !");
            } 
        } catch (NullPointerException e) {
            System.out.println("test.equals(\"\"): " + e);
        }
    }

    public static void error() {
        String test = null;
        if ("".equals(test)) {
            System.out.println("test = null, \"\".equals(test): true");
        } else {
            System.out.println("test = null, \"\".equals(test): false");
        }
    }

    public static void error2() {
        String test = null;
        if (test == "") {
            System.out.println("test = null, test == \"\": true");
        } else {
            System.out.println("test = null, test == \"\": false");
        }
    }

    public static void right() {
        String test = null;
        if (test == null) {
            System.out.println("test = null, test == null: true");  
        }
    }

    public static void right2() {
        String test = "not null";
        if (test.equals(null)) {
            System.out.println("test is null");
        }
        if (test.length() <= 0) {
            System.out.println("test is null");
        }
        if (test.isEmpty()) {
            System.out.println("test is null");
        }
        if (test == null) {
            System.out.println("test is null");
        }
        if (test != null && !test.isEmpty() && test.length() > 0 && !test.equals(null)) {
            System.out.println("test : " + test);
        }
    }

    public static void right3() {
        String test = "";
        if (test.equals("") && test == "") {
            System.out.println("test = \"\"");
        }
    }

    public static void main(String[] args) {
        Test.nullPoint1();
        Test.nullPoint2();
        Test.nullPoint3();
        Test.nullPoint4();
        Test.error();
        Test.error2();
        Test.right();
        Test.right2();
        Test.right3();
    }   
}

运行结果为:
test.equals(null): java.lang.NullPointerException
test.length() == 0: java.lang.NullPointerException
test.isEmpty(): java.lang.NullPointerException
test.equals(“”): java.lang.NullPointerException
test = null, “”.equals(test): false
test = null, test == “”: false
test = null, test == null: true
test : not null
test = “”


可以看到当字符串为null时,用equals()、length()、isEmpty() 都会抛出异常;而用与 “” 进行比较时,会出现判断失误。
所以可以使用 “if (test == null)” 来判断,简单直接。
注意当字符串为 “” 时,与null不同,不是空指针,得与”“比较。字符串为空

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值