现在的位置: 首页 > 综合 > 正文

Java中的==和equals

2017年12月19日 ⁄ 综合 ⁄ 共 873字 ⁄ 字号 评论关闭

 

  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

抱歉!评论已关闭.