
- 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
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java - Character.Subset hashCode() Method
The Java Character.Subset hashCode() method retrieves the standard hash code of the subset. A hash code is an integer value that represents a character or an input given to a hash function.
This method goes hand in hand with the Java Character.Subset equals() method as same subsets must have same hashcodes. However, vice versa is not valid; same hashcodes do not belong to the same subsets or inputs. Hence, the final keyword is used to maintain the consistency of both the methods.
Note − This method uses the inheritance concept to create a Subset object as the constructor in the Character.Subset class has protected access. Therefore, Character.Subset class is extended from another public class whose constructor uses super() method to invoke the Object class that creates an object.
Syntax
Following is the syntax for Java Character.Subset hashCode() method
public final int hashCode()
Parameters
This method does not accept any parameters.
Return Value
This method returns a hash code value for this object.
Example
The following example shows the usage of Java Character.Subset hashCode() method. Here, we instantiate CharacterSubsetDemo class objects with a String name as their values. Then, the hashcode() method is invoked on this object to obtain the hashcode of this object.
package com.tutorialspoint; import java.lang.*; public class CharacterSubsetDemo extends Character.Subset { // constructor of super class CharacterSubsetDemo(String s) { super(s); } public static void main(String[] args) { CharacterSubsetDemo obj1 = new CharacterSubsetDemo("admin"); CharacterSubsetDemo obj2 = new CharacterSubsetDemo("webmaster"); CharacterSubsetDemo obj3 = new CharacterSubsetDemo("administrator"); // returns a hash code value int retval = obj1.hashCode(); System.out.println("Hash code of Object " + obj1 + " = " + retval); retval = obj2.hashCode(); System.out.println("Hash code of Object " + obj2 + " = " + retval); retval = obj3.hashCode(); System.out.println("Hash code of Object " + obj3 + " = " + retval); } }
Output
Let us compile and run the above program, this will produce the following result −
Hash code of Object admin = 1704856573 Hash code of Object webmaster = 705927765 Hash code of Object administrator = 366712642
Example
In the following example, we will instantiate two objects using the same input string and invoke the hashCode() method on the both objects. This method returns the hashcode of each object created.
import java.lang.*; public class HashCodeDemo extends Character.Subset { HashCodeDemo(String s) { super(s); } public static void main(String []args){ HashCodeDemo obj = new HashCodeDemo("hello"); int h = obj.hashCode(); System.out.println("Hashcode of the given object is " + h); int h1 = obj.hashCode(); System.out.println("Hashcode of the given object is " + h1); } }
Output
The output for the program above after compiling and running is given as follows −
Hashcode of the given object is 705927765 Hashcode of the given object is 705927765
Example
In the example below, we will check if the two different objects with same object reference will have the same hashcode, generated by the invoking hashCode() method.
import java.lang.*; public class HashCodeDemo extends Character.Subset { HashCodeDemo(String s) { super(s); } public static void main(String []args){ HashCodeDemo obj = new HashCodeDemo("admin"); HashCodeDemo obj1 = obj; boolean b = obj1.equals(obj); System.out.println(b); int h = obj.hashCode(); System.out.println("Hashcode of the given object is " + h); int h1 = obj1.hashCode(); System.out.println("Hashcode of the given object is " + h1); } }
Output
Let us compile and run the program above, the output is achieved as follows −
true Hashcode of the given object is 705927765 Hashcode of the given object is 705927765
Example
Suppose, the string name for the subset object is given as null, the following example shows how the hashCode() method works in such cases.
import java.lang.*; public class HashCodeDemo extends Character.Subset { HashCodeDemo(String s) { super(s); } public static void main(String []args){ HashCodeDemo obj1 = new HashCodeDemo(null); int h = obj1.hashCode(); System.out.println("Hashcode of the given object is " + h); } }
Exception
After compiling and executing the program above, the output displays a NullPointerException −
Exception in thread "main" java.lang.NullPointerException: nameat java.lang.Character$Subset.<init>(Character.java:615) at HashCodeDemo.<init>(HashCodeDemo.java:6) at HashCodeDemo.main(HashCodeDemo.java:10)