Méthode Java Integer class hashCode() avec exemple
Méthode hashCode() de la classe Integer
- méthode hashCode() est disponible dans le package java.lang.
- méthode hashCode() est utilisé pour renvoyer le hashcode de l'objet Integer.
- méthode hashCode() est une méthode non statique, elle est accessible uniquement avec l'objet de classe et si nous essayons d'accéder à la méthode avec le nom de la classe, nous obtiendrons une erreur.
- méthode hashCode() ne lève pas d'exception au moment de renvoyer le code de hachage.
Syntaxe :
public int hashCode();
Paramètre(s) :
- Il n'accepte aucun paramètre.
Valeur renvoyée :
Le type de retour de cette méthode est int, elle retourne le code de hachage pour cet objet.
Exemple :
// Java program to demonstrate the example // of int hashCode() method of Integer class public class HashCodeOfIntegerClass { public static void main(String[] args) { // Variables initialization int value1 = 30; int value2 = 10; // It returns hashcode value denoted by this Integer i1 object // by calling i1.hashCode() Integer i1 = new Integer(value1); // Display i1 result System.out.println("i1.hashCode(): " + i1.hashCode()); // It returns hashcode value denoted by this Integer i2 object // by calling i2.hashCode() Integer i2 = new Integer(value2); // Display i2 result System.out.println("i2.hashCode(): " + i2.hashCode()); } }
Sortie
i1.hashCode(): 30 i2.hashCode(): 10