Java中的==和equals

来源:互联网 发布:麦克毕比生涯数据 编辑:程序博客网 时间:2024/06/03 12:41

 

  1. public static void abc() { 
  2.     String abc = null
  3.     try { 
  4.         if (abc.equals(null)) 
  5.             System.out.println("1"); 
  6.         abc = "abc"
  7.         if (abc == "abc"
  8.             System.out.println("2"); 
  9.     } catch (Exception e) { 
  10.         System.out.println("3"); 
  11.     } finally { 
  12.         System.out.println("4"); 
  13.     } 
  14.     System.out.println("5"); 
结果:

3

4

5

在第4行的时候报了java.lang.NullPointerException异常

 

如果是下面这样

  1. public static void abc() { 
  2.     String abc = null
  3.     try { 
  4.         if (abc.equals(null)) 
  5.             System.out.println("1"); 
  6.     } catch (Exception e) { 
  7.         e.printStackTrace(); 
  8.         abc = "abc"
  9.         if (abc == "abc"
  10.             System.out.println("2"); 
  11.         System.out.println("3"); 
  12.     } finally { 
  13.         System.out.println("4"); 
  14.     } 
  15.     System.out.println("5"); 

结果

2

3

4

5

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1182155

0 0