简体   繁体   English

java中==,equals和hashcode的示例

[英]Example of ==, equals and hashcode in java

Given this: 鉴于这种:

String s1= new String("abc");
String s2= new String("abc");
String s3 ="abc";
System.out.println(s1==s3);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());

Output is: 输出是:

false
false
true
true
96354
96354
96354

Here == is giving false for each object but the hashcode for each String object is same. 这里==为每个对象赋予false,但每个String对象的哈希码是相同的。 Why is it so? 为什么会这样?

== does compare real equality of objects (I mean - both references point to the same object), not their content, whereas .equal compares content (at least for String). ==确实比较对象的真实相等性(我的意思是 - 两个引用指向同一个对象),而不是它们的内容,而.equal比较内容(至少对于String)。

String a = new String("aa");
String b = new String("aa"); 

a and b are pointing to different objects. ab指向不同的对象。

Notice also that if objects are equal then their hashchodes must be the same, but if hashcodes are the same, it doesn't mean that objects are equal. 另请注意,如果对象相等,则它们的哈希序列必须相同,但如果哈希码相同,则并不意味着对象相等。

The equals contract says that if o1.equals(o2) , then o1.hashCode() == o2.hashCode() . equals合同说如果是o1.equals(o2) ,那么o1.hashCode() == o2.hashCode() It doesn't specify anything about the hash codes of unequal objects. 它没有指定任何关于不等对象的哈希码的内容。 You could have a method like 你可以有一个类似的方法

public int hashCode()
{
    return 42;
}

and it'd fulfill the contract. 它履行了合同。 It's just expected that the hash code be related to the value of the object, in order to make hash tables work more efficiently. 只是期望哈希码与对象的值相关,以使哈希表更有效地工作。

Now, as for why your == doesn't work, two objects will always be compared by reference. 现在,至于为什么你的==不起作用,将始终通过引用比较两个对象。 That is, if o1 == o2 , then o1 and o2 are the exact same object. 也就是说,如果o1 == o2 ,则o1o2是完全相同的对象。 That's rarely what you want; 这很少是你想要的; you usually want to see if o1.equals(o2) instead. 你通常想看看是否o1.equals(o2)

When you use == , you are comparing if two variables hold reference to the same Object. 当您使用== ,您将比较两个变量是否保持对同一个Object的引用。 In other words s1 == s2 is like asking: are the s1 and s2 variables referring to the same String object? 换句话说, s1 == s2就像问: s1s2变量是否引用相同的String对象? And that's not true, even when both String objects have the same "abc" value. 即使两个String对象具有相同的“abc”值,情况也是如此。

When you use equals(), you are comparing the value of both objects. 使用equals()时,您正在比较两个对象的值。 Both objects may not be the same, but their value (in this case "abc") is the same, so it returns true . 两个对象可能不相同,但它们的值(在本例中为“abc”)是相同的,因此它返回true

How do you define whether an object is equal to another? 如何定义一个对象是否与另一个对象相等? That's up to you. 随你(由你决定。 In this case the String object already defines this for you, but for example if you define a Person object, how do you know if a person P1 is equal to P2? 在这种情况下,String对象已经为您定义了这个,但是例如,如果您定义了Person对象,您如何知道人P1是否等于P2? You do that by overriding equals() and hashCode() . 你可以通过重写equals()hashCode()做到这一点。

== tells you whether the two variable references point at the same object in memory, nothing more. ==告诉你两个变量引用是否指向内存中的同一个对象,仅此而已。 equals() and hashCode() both look at the contents of the object and each uses its own algorithm for calculation. equals()hashCode()都查看对象的内容,每个都使用自己的算法进行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM