- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character hashCode() Method
The Java Character hashCode() method returns a hash code for the given Character. A hash code is a unique primitive integer that is assigned to every object in Java.
Note −
This method in Character class overrides the hashCode() method in Object class.
The result obtained when this method is invoked is the same as the result obtained when the charValue() method is invoked.
There are two polymorphic forms of this method. One takes no arguments while the other takes a char value as its argument.
Syntax
Following is the syntax for Java Character hashCode() method
public int hashCode() or, public static int hashCode(char value)
Parameters
value − the char value for which a hash code must be returned.
Return Value
This method returns a hash code value for a Character.
Example
The following example shows the usage of Java Character hashCode() method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 Character objects c1, c2 Character c1, c2; // assign values to c1, c2 c1 = new Character('z'); c2 = new Character('Z'); // create 2 int primitives i1, i2 int i1, i2; // assign hashcodes of c1, c2 to i1, i2 i1 = c1.hashCode(); i2 = c2.hashCode(); String str1 = "Hashcode of " + c1 + " is " + i1; String str2 = "Hashcode of " + c2 + " is " + i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } }
Output
Let us compile and run the above program, this will produce the following result −
Hashcode of z is 122 Hashcode of Z is 90
Example
Another example of this method that takes various types of character values can be seen below −
import java.lang.*; public class CharacterDemo { public static void main(String args[]) { Character c1 = new Character('A'); Character c2 = new Character('a'); Character c3 = new Character('7'); Character c4 = new Character('@'); int res = c1.hashCode(); System.out.println("The hash code of given character is " + res); res = c2.hashCode(); System.out.println("The hash code of given character is " + res); res = c3.hashCode(); System.out.println("The hash code of given character is " + res); res = c4.hashCode(); System.out.println("The hash code of given character is " + res); } }
Output
Compile and run the program given above and the output for it is displayed as follows −
The hash code of given character is 65 The hash code of given character is 97 The hash code of given character is 55 The hash code of given character is 64
Example
Now let us see the usage of the Java Character hashCode(char ch) method.
import java.lang.*; public class Demo { public static void main (String args[]) { Character c1 = new Character('?'); int hc1 = Character.hashCode(c1); System.out.println("The hash code of Character is " + hc1); Character c2 = new Character('x'); int hc2 = Character.hashCode(c2); System.out.println("The hash code of Character is " + hc2); Character c3 = new Character('6'); int hc3 = Character.hashCode(c3); System.out.println("The hash code of Character is " + hc3); Character c4 = new Character('+'); int hc4 = Character.hashCode(c4); System.out.println("The hash code of Character is " + hc4); } }
Output
On compiling and running the code above, we achieve the following output −
The hash code of Character is 63 The hash code of Character is 120 The hash code of Character is 54 The hash code of Character is 43