Java Character hashCode() Example

Java is an object-oriented language where each real life entity can be referred as an object. Towards this goal, Java comes with java.lang.Integer class to represent primitive data type int as an object, class to represent a simple system address like as an object, class to give a hard disk file an object form, java.lang.String class to to represent some name an object form, java.util.Date to represent system date as an object and finally java.lang.Character class to give data type char, an object form etc.

For example,

char ch = 'A';
Character c1 = new Character(ch);

Wherever, you would like to use char ch as an object in Java coding, use c1. Object c1 practically represents char ch as an object. That is, c1 wraps around ch to give ch an object form. For this reason, Character class is known as wrapper class for primitive data type char.

Following is the signature of Character class as defined in java.lang package.

public final class Character extends Object implements Serializable, Comparable

Character class was introduced in Java API with JDK 1.0, the starting version of Java.

The Character class provides many methods to find for what type the character belongs like lowercase letter or a digit etc. and few methods to convert characters from one type to another like lowercase to uppercase etc.

What is hash code?

hashCode() is a method of Object class. Hash code is an integer representation of an object by JVM. Hash code is system generated using some formula (let us not go much of detais of formula). Hash codes generated need not be the same for different times of execution. Internally, the objects are stored in Hashtable format. The equals() method, as we know earlier, compares two strings or characters. For comparison, the JVM uses their hash codes only. This way of comparison is very faster than comparing each character in the two strings. This hash code format cannot be used by the programmer in coding.

Following is the method signature as defined Character class

  • public int hashCode(): Returns the hash code (here, ASCII value) of the char represented by this Character object.

With this introduction let us go to an example.

Following Java Character hashCode() Example illustrates.
public class CharacterFunctionDemo
  public static void main(String args[])
    Character c1 = new Character('A');
    Character c2 = new Character('A');
    Character c3 = new Character('B');

    int hc1 = c1.hashCode();
    int hc2 = c2.hashCode();
    int hc3 = c3.hashCode();

    System.out.println("Hash code of 'A' of Character object c1: " + hc1); 
    System.out.println("Hash code of 'A' of Character object c2: " + hc2); 
    System.out.println("Hash code of 'B' of Character object c3: " + hc3); 

    System.out.println("\nHash codes of c1 and c2 representing 'A' are same: " + (hc1 == hc2)); 
    System.out.println("Hash codes of c1 and c3 representing 'A' and 'B' are same: " + (hc1 == hc3)); 

Java Character hashCode() Example
Output screen of Java Character hashCode() Example

For practice another example on hashcode is available but with strings.

Leave a Comment

Your email address will not be published.