String a = "success";
String b = "success";
System.out.println(a.hashCode());
System.out.println(b.hashCode());
if(a.equals(b)){
System.out.println("123");
}
I can't understand why the two string have different hashcode.
我无法理解为什么这两个字符串有不同的哈希码。
String a = "success";
String b = "success";
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(b));
output:
-1867169789
1954952228
33263331
6413875
3 个解决方案
#1
You've inserted the zero width no-break space (U+FEFF) character at the beginning of your second string.
您已在第二个字符串的开头插入了零宽度不间断空格(U + FEFF)字符。
That string is actually equal to the following string (no hidden unicode characters): "\ufeffsuccess"
该字符串实际上等于以下字符串(没有隐藏的unicode字符):“\ ufeffsuccess”
That means a
and b
are not equal and do not have the same hash code.
这意味着a和b不相等,并且没有相同的哈希码。
#2
public class Compare {
public static void main(String args[]) {
String a = "success";
String b = "success";
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
for(int i = 0; i < aChar.length; i++)
{
System.out.println((int)aChar[i]);
}
System.out.println("");
for(int i = 0; i < bChar.length; i++)
{
System.out.println((int)bChar[i]);
}
}
}
The first char of the second string is char 65279. Did you copy the strings from somewhere?
第二个字符串的第一个字符是char 65279.你是否从某处复制了字符串?
The Strings look the same, but are not. Try typing the "success" text again, then it should work.
字符串看起来一样,但不是。尝试再次输入“成功”文本,然后它应该工作。
#3
When I copied and pasted your code into my IDE, it showed that the opening double quotes of b = "success is actually a weird double quote rather than a standard double quote. Bizzarrely, I got the same result as you - code compiles and runs, but outputs different hash codes. By fixing the double quote with a normal double quote, the hashcodes are now the same.
当我将你的代码复制并粘贴到我的IDE中时,它显示b =“成功的开头双引号实际上是一个奇怪的双引号,而不是标准的双引号.Botzarrely,我得到了与你相同的结果 - 代码编译和运行,但输出不同的哈希码。通过使用普通双引号修复双引号,哈希码现在是相同的。
#1
You've inserted the zero width no-break space (U+FEFF) character at the beginning of your second string.
您已在第二个字符串的开头插入了零宽度不间断空格(U + FEFF)字符。
That string is actually equal to the following string (no hidden unicode characters): "\ufeffsuccess"
该字符串实际上等于以下字符串(没有隐藏的unicode字符):“\ ufeffsuccess”
That means a
and b
are not equal and do not have the same hash code.
这意味着a和b不相等,并且没有相同的哈希码。
#2
public class Compare {
public static void main(String args[]) {
String a = "success";
String b = "success";
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
for(int i = 0; i < aChar.length; i++)
{
System.out.println((int)aChar[i]);
}
System.out.println("");
for(int i = 0; i < bChar.length; i++)
{
System.out.println((int)bChar[i]);
}
}
}
The first char of the second string is char 65279. Did you copy the strings from somewhere?
第二个字符串的第一个字符是char 65279.你是否从某处复制了字符串?
The Strings look the same, but are not. Try typing the "success" text again, then it should work.
字符串看起来一样,但不是。尝试再次输入“成功”文本,然后它应该工作。
#3
When I copied and pasted your code into my IDE, it showed that the opening double quotes of b = "success is actually a weird double quote rather than a standard double quote. Bizzarrely, I got the same result as you - code compiles and runs, but outputs different hash codes. By fixing the double quote with a normal double quote, the hashcodes are now the same.
当我将你的代码复制并粘贴到我的IDE中时,它显示b =“成功的开头双引号实际上是一个奇怪的双引号,而不是标准的双引号.Botzarrely,我得到了与你相同的结果 - 代码编译和运行,但输出不同的哈希码。通过使用普通双引号修复双引号,哈希码现在是相同的。