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

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

java - Log4j2 configuration not found when running standalone application builded by shade plugin -

python - How do I create a list index that loops through integers in another list -