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

相关文章
相关标签/搜索