哈希码值为什么相同?哈希码值相同
public static void main(String args[])
{
String s1="abc";
String s2=new String("abc");
System.out.println("Hashcode s1-:"+ s1.hashCode());
System.out.println("Hashcode s2-:"+ s2.hashCode());
if(s1==s2){
System.out.println("==true:");
}
}
输出
Hashcode s1-:96354
Hashcode s2-:96354
因为字符串是相同的。 – 2013-03-18 09:27:51
在这里看到更多的信息http://stackoverflow.com/questions/785091/consistency-of-hashcode-on-a-java-string – 2013-03-18 09:28:34
@Sudhanshu他呼吁新的String,所以他们不是指同一个对象,因此,为什么==返回false。 – DaveJohnston 2013-03-18 09:29:37