Java Character hashCode Method

JavaJavaBeginner
Practice Now

Introduction

The hashCode(char ch) method is used to return the hash code of the char value passed as the argument. It returns a unique integer value (hash code) associated with the char value passed as an argument.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117501{{"`Java Character hashCode Method`"}} java/classes_objects -.-> lab-117501{{"`Java Character hashCode Method`"}} java/class_methods -.-> lab-117501{{"`Java Character hashCode Method`"}} java/exceptions -.-> lab-117501{{"`Java Character hashCode Method`"}} java/modifiers -.-> lab-117501{{"`Java Character hashCode Method`"}} java/oop -.-> lab-117501{{"`Java Character hashCode Method`"}} java/packages_api -.-> lab-117501{{"`Java Character hashCode Method`"}} java/user_input -.-> lab-117501{{"`Java Character hashCode Method`"}} java/wrapper_classes -.-> lab-117501{{"`Java Character hashCode Method`"}} java/identifier -.-> lab-117501{{"`Java Character hashCode Method`"}} java/arrays -.-> lab-117501{{"`Java Character hashCode Method`"}} java/data_types -.-> lab-117501{{"`Java Character hashCode Method`"}} java/operators -.-> lab-117501{{"`Java Character hashCode Method`"}} java/output -.-> lab-117501{{"`Java Character hashCode Method`"}} java/strings -.-> lab-117501{{"`Java Character hashCode Method`"}} java/variables -.-> lab-117501{{"`Java Character hashCode Method`"}} java/object_methods -.-> lab-117501{{"`Java Character hashCode Method`"}} java/system_methods -.-> lab-117501{{"`Java Character hashCode Method`"}} end

Create a file

Create a new file named CharHashCodeDemo.java in the ~/project directory using the following command.

touch ~/project/CharHashCodeDemo.java

Write code to generate hash code

Open the file CharHashCodeDemo.java in your favorite text editor and write the following code.

import java.util.Scanner;

public class CharHashCodeDemo {

    public static void main(String[] args) {

        try {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a character: ");
            char ch = sc.next().charAt(0);

            int hash = Character.hashCode(ch);

            System.out.println("Hash code of character " + ch + " is: " + hash);
        }catch(Exception e) {

            System.out.println("Invalid input! Please try again...");
        }
    }
}

This code will accept a character value from the user and generate the hash code associated with that character.

Compile and run the code

Open your terminal and navigate to the ~/project directory using the following command.

cd ~/project

Compile the code using the following command.

javac CharHashCodeDemo.java

Run the code using the following command.

java CharHashCodeDemo

After running the code, you will get the following message in the terminal.

Enter a character:

Type a character value and hit the Enter key. You will get the hash code of the character in the terminal.

Summary

That's it! In this lab, you have learned how to generate unique hash code values of a given character using the hashCode(char ch) method of the Character class. You have created a Java code file that accepts a character value from the user and generates the hash code associated with that character. You have also learned how to compile and run the Java code file in the terminal of the Ubuntu system.

Other Java Tutorials you may like