结果:
- public static void abc() {
- String abc = null;
- try {
- if (abc.equals(null))
- System.out.println("1");
- abc = "abc";
- if (abc == "abc")
- System.out.println("2");
- } catch (Exception e) {
- System.out.println("3");
- } finally {
- System.out.println("4");
- }
- System.out.println("5");
- }
3
4
5
在第4行的时候报了java.lang.NullPointerException异常
如果是下面这样
- public static void abc() {
- String abc = null;
- try {
- if (abc.equals(null))
- System.out.println("1");
- } catch (Exception e) {
- e.printStackTrace();
- abc = "abc";
- if (abc == "abc")
- System.out.println("2");
- System.out.println("3");
- } finally {
- System.out.println("4");
- }
- System.out.println("5");
- }
结果
2
3
4
5
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1182155