JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label equals(). Show all posts
Showing posts with label equals(). Show all posts

hashcode() and equals() method in java

3:28 AM
hashcode() and equals() method in java These methods can be found in the Object class and hence available to all java classes.Using these two methods, an object can be stored or retrieved from a Hashtable, HashMap or HashSet. hashcode() equals() hashcode() and equals() method hashcode(): You might know if you put entry in HashMap, first hashcode is calculated and this hashcode used to find bucket(index) where this entry will get stored in hashMap.You can read more at  How hashMap works in java . What if you don’t override hashcode method, it will  return integer representation of memory address. equals(): You have to override equals method, when you want to define equality between two object. If you don’t override this method, it will check for reference equality(==) i.e. if tow reference refers to same object or not Lets override default implemenation of hashcode() and equals(): You don’t have to always override these methods, but lets say you want to defi

.