Java中的==和equals
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
积跬步以致千里,积小流以成江海。
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!

浙公网安备 33010602011771号