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' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -