java對象相等

https://www.dutycode.com/post-140.htmlhtml

簡單來首,Object方法裏的equals也是直接判斷兩個引用是否指向同一個地址,即引用同一個對象java

public boolean equals(Object obj) {
        return (this == obj);
    }

  通常狀況下須要本身重寫equals方法。post

 

先看stringthis

        String s1=new String("test");
        String s2=new String("test");
        System.out.println("s1:"+(s1.hashCode()));
        System.out.println("s2:"+(s2.hashCode()));
        System.out.println(s1.equals(s2)+" "+(s1==s2));

  

s1:3556498
s2:3556498
true falsecode

對string來講,值同樣,hashcode就同樣htm

 

在集合裏判斷類相等通常會涉及到hashCode對象

集合裏判斷相等的機制blog

https://www.cnblogs.com/Latiny/p/8359088.htmlstring

相關文章
相關標籤/搜索