Java >> Tutoriel Java >  >> Tag >> class

Méthode Java Short class hashCode() avec exemple

Méthode hashCode() de classe courte

  • méthode hashCode() est disponible dans le package java.lang.
  • méthode hashCode() est utilisé pour renvoyer le hashcode de l'objet Short.
  • 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 Short class

public class HashCodeOfShortClass {
    public static void main(String[] args) {
        // Variables initialization
        short value1 = 30;
        short value2 = 10;

        // It returns hashcode value denoted by this Short ob1 object
        // by calling ob1.hashCode()
        Short ob1 = new Short(value1);

        // Display ob1 result
        System.out.println("ob1.hashCode(): " + ob1.hashCode());

        // It returns hashcode value denoted by this Short ob2 object
        // by calling ob2.hashCode()
        Short ob2 = new Short(value2);

        // Display ob2 result
        System.out.println("ob2.hashCode(): " + ob2.hashCode());
    }
}

Sortie

ob1.hashCode(): 30
ob2.hashCode(): 10

Balise Java