判断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不同,不是空指针,得与”“比较。字符串为空

你可能感兴趣的:(Java)