[英]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. 在上面的代码中,
s1
和s2
的内容相同(“cat”),因此返回的整数是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.