java - Hashcode value is same -


why hashcode value same?

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:");     }  } 

output

hashcode s1-:96354 hashcode s2-:96354 

the hash code 2 equal objects should equal.

in case, objects strings , considered equal because hold same sequence of characters, "abc".

if want hash code based on object identity rather equality use system.identityhashcode().


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -