java 的 equals 和 ==

① String s1 = "hello"

   String s2 = "hello"


   s1 == s2

   s1 equals s2


② String s1 = "hello"

   String s2 = new String("hello")


   s1 != s2

   s1 equals s2


③ String s1 = "hello"

   String s2 = new String("hello")

   s2 = s2.intern()


   s1 == s2

   s1 equals s2


④ int a1 = 1

   int a2 = 1

   Integer b1 = new Integer(1)

   Integer b2 = new Integer(1)


   a1 == a2

   a1 == b1  false (jdk1.5以上 true)

   b1 == b2  false 

   b1 equals b2



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章