简体   繁体   English

为什么哈希码对于String s1 =“cat”和String s2 = new String(“cat”)是相同的?

[英]Why hash code is same for String s1= “cat” and String s2= new String(“cat”)?

Program : 计划:

 class JavaCode
     {
     public static void main (String[] args) throws java.lang.Exception
      {
        String s1 ="cat";
        String s2 = new String("cat");
        System.out.println(s1 == s2);
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
     }
  }

Output : 输出:

false
98262
98262

If S1 and S2 are pointing to different memory address, then Hash code should be different for them? 如果S1和S2指向不同的内存地址,那么哈希代码对它们应该是不同的? Please Explain how they are same? 请解释它们是如何相同的?

If S1 and S2 are pointing to different memory address, then Hash code should be different for them?

No, this is not how hash codes work. 不,这不是哈希码的工作方式。 If two objects are equal , their hash code MUST be equal too. 如果两个对象equal ,它们的哈希码也必须相等。 It doesn't matter "where in memory" they sit. 他们坐在“记忆中的位置”并不重要。

I recommend reading through the following article: http://www.javaworld.com/article/2074996/hashcode-and-equals-method-in-java-object---a-pragmatic-concept.html 我建议阅读以下文章: http//www.javaworld.com/article/2074996/hashcode-and-equals-method-in-java-object---a-pragmatic-concept.html

A hashcode is based on the content of some object. 哈希码基于某个对象的内容

Whereas == compares references, or in other words: "positions" in memory. 而= =比较引用,或换句话说:“位置”在内存中。

Thus two objects can very well have the same hashcode() (because, well: same content); 因此,两个对象可以很好地具有相同的hashcode() (因为,井:相同的内容); but belong to two different references. 但属于两个不同的参考。

And that by the way why you always always always compare Strings using the equals() method; 顺便说一下,为什么你总是总是使用equals()方法比较字符串; and not ==. 而不是==。

From the documentation of hashCode() : hashCode()文档

Returns a hash code for this string. 返回此字符串的哈希码。 The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. String对象的哈希码使用int算法计算为s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] ,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示取幂。

Since both string has same values in it, hashcode are same. 由于两个字符串都具有相同的值,因此哈希码是相同的。

c has ASCII value 99, a has 97 and t has 116. So, c的ASCII值为99,a为97,t为116.所以,

hashcode of s1 = 99 * 31^2 + 97 * 31^1 + 116 = 95139 + 3007 + 116 = 98262.

Also hashcode for s2 will be 98262. That's how equal values makes hashcode same. s2的哈希码也将是98262.这就是等值使哈希码相同的方式。

hashCode() is the public instance method of String class, which return the integer value based on the content. hashCode()String类的公共实例方法,它根据内容返回整数值。

    class JavaCode
     {
     public static void main (String[] args) throws java.lang.Exception
      {
        String s1 ="cat";
        String s2 = new String("cat");
        System.out.println(s1 == s2);
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
      }
     }

In the above code, the content of s1 and s2 is same ("cat"), so the integer which is being returned is same. 在上面的代码中, s1s2的内容相同(“cat”),因此返回的整数是相同的。

暂无
暂无

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

相关问题 String s1 == String s2(true)但FieldOffset不同 - String s1 == String s2 (true) but FieldOffset is different 使用可比较或比较器接口使用String s1的顺序对String s2进行排序 - Sort String s2 using the order of String s1 using either of comparable or comparator interface 尝试设计一个函数:hasCheated(String s1,String s2,int N)求值 - Trying to design a function : hasCheated(String s1,String s2, int N) that evaluates 为什么在以下代码中我们无法为字符串s1提供输入? - Why are we not able to give input to the string s1 in the following code? String s = s1 + s2;(s1和s2是字符串文字)从何处返回? 堆或池 - Where does String s= s1+s2;(s1 & s2 are String literals) returns from? heap or pool 在连接两个String s1和s2时,生成的输出String s3将在java中创建堆或常量池吗? - on concatenating two String s1 and s2, produced output String s3 will create in heap or constant pool in java? 为什么选择System.out.println(“嘿s1 == s2:”+ s1 == s2); 打印“false”作为输出而不是打印“hey s1 == s2:false” - Why System.out.println(“hey s1==s2:”+s1==s2); prints “false” as the output instead of printing “hey s1==s2:false” 如何更改Student类,以便当s1 = new Student()和s2 = new Student()时,s1 == s2返回true? - How to change Student class so that s1 == s2 return true when s1 = new Student() and s2 = new Student()? 找到字符串S2的时间 - Find time taken to string S2 S1包含带有正则表达式的s2 - S1 contains s2 with regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM