java 字符串为空问题

时间:2022-07-19 18:44:36

java 字符串为空问题

            String testStr = null;
System.out.println(testStr); if (testStr == null) {
System.out.println("testStr == null"); // 能正常打印 } if (testStr.equals("null")) {
System.out.println("testStr.equals null "); //Exception in thread "main" java.lang.NullPointerException }
           String testStr="";
System.out.println("aaaa"+testStr); //能打印 if (testStr == null) {
System.out.println("testStr == null"); //不能打印 } if (testStr.equals("null")) {
System.out.println("testStr.equals null "); ////不能打印 }         System.out.println("length:"+testStr.length()); // 打印length:0