java - difference between equals() and hashCode() -


i want brief definition equals() , "==" , hashcode(). if run following code means output "true false 2420395 2420395". had understand equals() method compares string , "==" compares reference. in output hashccode() method prints reference number both strings same why "==" returns "false".

            string str = "name";     string str1 = new string("name");      if(str.equals(str1))         system.out.println("true");     else         system.out.println("false");     if(str==str1)         system.out.println("true");     else         system.out.println("false");      system.out.println(str.hashcode());     system.out.println(str1.hashcode()); } 

hashcode() not return object's reference, hash of object, computed in way. == not compare objects using value of hashcode() but, correctly say, value of objects' references.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -