java包裝類的比較

java包裝類比較注意事項:

  1. 包裝類對象直接比較的值是對象地址
  2. 若要進行value值的比較需要獲取真實值,或者直接調用equals方法

code:

	@Test
    public void basicTest() {
        Long t1 = new Long(1L);
        Long t2 = new Long(1L);
        System.out.println("t1.longValue() == t2.longValue() " + (t1.longValue() == t2.longValue()));
        System.out.println("t1 = t2 " + (t1 == t2));
        Integer a1 = new Integer(1);
        Integer a2 = new Integer(1);
        System.out.println("a1 == a2 " + (a1 + a2));
        System.out.println("a1.equals(a2) " + (a1.equals(a2)));
        String s1 = new String("test");
        String s2 = new String("test");
        System.out.println("s1 == s2 " + (s1 == s2));
        System.out.println("s1.equals(s2) " + (s1.equals(s2)));
    }

result:

t1.longValue() == t2.longValue() true
t1 = t2 false
a1 == a2 false
a1.equals(a2) true
s1 == s2 false
s1.equals(s2) true
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章