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

Google sheets equipment borrowing system -

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

c# - Convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.IList<>' -