
- 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 - Short hashCode() Method
The Java Short hashCode() method retrieves the hash code for the given Short object. Every object in Java has an associated hash code, which is a unique primitive integer value. The value retrieved by this method is same as the value returned by the intValue() method.
Objects or entities are mapped to integer values using the fundamental computer science notion of hashing. These values are referred to as the entities' hash values or hashcodes.
This method has two polymorphic variants. The syntaxes of these variants are discussed below.
Syntax
Following is the syntax for Java Short hashCode() method −
public int hashCode() or, public static int hashCode(short x)
Parameters
x − It is the value to the hash
Return Value
Following is an example which shows the usage of Java Short hashCode() method. Here we are creating a Short object with a positive value. Thereafter printing the hash code using this method −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { short value1 = 8487; Short value2 = new Short(value1); // The hash code method int value3 = value2.hashCode(); System.out.println("The Hash code value is = " + value3); // The hash code static method Short value4 = new Short("8487"); System.out.println("The short value is = " + Short.hashCode(value4)); } }
Output
Let us compile and run the above program, this will produce the following result −
The Hash code value is = 8487 The short value is = 8487
Example
Following is another example of the hashCode() method −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { // create the Short objects Short value1 = 87; Short value2 = 987; // print the hashcode of the Short objects System.out.println("hashCode of value1 is: " + value1.hashCode()); System.out.println("hashCode of value2 is: " + value2.hashCode()); } }
Output
Following is an output of the above code −
hashCode of value1 is: 87 hashCode of value2 is: 987
Example
In the following is an example we are trying to find the hashcode of a negative value −
public class ShortDemo { public static void main(String[] args) { // passing the negative integer Short value1 = new Short("-36"); System.out.println("Hash Value of negative integer is = " + value1.hashCode()); } }
Output
On executing the program above, the output is obtained as follows −
Hash Value of negative integer is = -36
Example
In the following code a negative and a positive short value is passed as an argument to print the equivalent hash code value of the values passed −
public class ShortDemo { public static void main(String[] args) { short s1 = 987; short s2 = -786; // returning the hash code value int h1 = Short.hashCode(s1); int h2 = Short.hashCode(s2); System.out.println("hash code value of s1 is: " + h1); System.out.println("hash code value of s2 is: " + h2); } }
Output
While executing the above code, we get the following output −
hash code value of s1 is: 987 hash code value of s2 is: -786