Java.lang.Enum.hashCode() Method

Advertisements


Description

The java.lang.Enum.hashCode() method returns a hash code for this enum constant.

Declaration

Following is the declaration for java.lang.Enum.hashCode() method

public final int hashCode()

Parameters

  • NA

Return Value

This method returns a hash code for this enum constant.

Exception

  • NA

Example

The following example shows the usage of java.lang.Enum.hashCode() method.

package com.tutorialspoint;

import java.lang.*;

// enum showing programming languages
enum Language {
   C, Java, PHP;
}

public class EnumDemo {

   public static void main(String args[]) {
       
     // returns the name and hashCode of this enum constant
     System.out.print("Programming in " + Language.C.toString());      
     System.out.println(", Hashcode = " + Language.C.hashCode()); 
     System.out.print("Programming in " + Language.Java.toString());  
     System.out.println(", Hashcode = " + Language.Java.hashCode()); 
     System.out.print("Programming in " + Language.PHP.toString());  
     System.out.println(", Hashcode = " + Language.PHP.hashCode());    
   }
} 

Let us compile and run the above program, this will produce the following result:

Programming in C, Hashcode = 1569228633
Programming in Java, Hashcode = 778966024
Programming in PHP, Hashcode = 1021653256



Advertisements
Advertisements