关于String.equals(null)

public class Than {
    public static void main(String[] args) {
        fun(null,"abc");
    }

    public static void fun(String i, String j) {
        //String = String
        try {
            if(i == j) {
                System.out.println("执行");
            }
        }catch(Exception ex) {
            System.out.println("============");
            ex.printStackTrace();
        }finally {
            System.out.println("结束");
        }
        System.out.println("+++++++++++++");
        //String.equals(String)
        try {
            if(i.equals(j)) {
                System.out.println("执行");
            }
        }catch(Exception ex) {
            System.out.println("============");
            ex.printStackTrace();
        }finally {
            System.out.println("结束");
        }
        System.out.println("+++++++++++++");
        //String.equals(String)
        try {
            if(j.equals(i)) {
                System.out.println("执行");
            }
        }catch(Exception ex) {
            System.out.println("============");
            ex.printStackTrace();
        }finally {
            System.out.println("结束");
        }
    }
}
结束
+++++++++++++
============
结束
+++++++++++++
结束
java.lang.NullPointerException
    at trycatch.Than.fun(Than.java:23)
    at trycatch.Than.main(Than.java:5)

你可能感兴趣的:(***博学广记***,***博学多才***)