Learn Java Programming - Overriding the .hashCode() method Tutorial

Publié le: 14 janvier 2016
sur la chaîne: Daniel Ross
2,240
27

The Object class contains eleven methods; over half of those methods are somehow dependent upon other methods within the Object class to function properly. In a previous tutorial I discussed the relationship between the .wait() method and the .notify() and .notifyAll() methods. One of the most widely used methods, .equals(), shares a special relationship with the .hashcode() method. At this point you should already be familiar with invoking the .equals() method on an object. The purpose of this tutorial is to explain what a hash code is and how to properly override the .hashCode() method.

So what is a hash code? A hash code is a number, and in Java it can simply be any number that fits into the primitive int range of -2147483648 to 2147483647.
What purpose does a hash code serve? The purpose of a hash code is to provide a quick way of locating an object when it is mixed in with a bunch of other objects (clear as mud right???).
Without realizing it, we use hash codes all over the place in everyday life. Imagine that you have a craving for a can of Coca-Cola (Coke) so you head over the grocery store to get one. When you step into the grocery store you are presented with a bunch of stuff for sale. That "stuff" is comprised of thousands of objects; a six-pack of Coke is an object and it the specific object that you are searching for amongst a sea of other objects.
So where to we begin? We'll just start walking around hoping to stumble across it and we will locate it eventually after some time has elapsed.


Sur cette page du site, vous pouvez voir la vidéo en ligne Learn Java Programming - Overriding the .hashCode() method Tutorial durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Daniel Ross 14 janvier 2016, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 2,240 fois et il a aimé 27 téléspectateurs. Bon visionnage!