Javatpoint Logo
Javatpoint Logo

Java Integer hashCode() Method

The hashCode() method is a Java Integer class method which returns the hash code for the given inputs. There are two different types of Java hashCode() method which can be differentiated depending on its parameter.

These are:

  1. Java Integer hashCode() Method
  2. Java Integer hashCode(int value) Method

hashCode() Method

The hashCode() is a method of Java Integer Class which determines the hash code for a given Integer. It overrides hashCode in class Object. By default, this method returns a random integer that is unique for each instance.

hashCode(int value) Method

The hashCode(int value) is an inbuilt Java Integer Class method which determines a hash code for a given int value. This method is compatible with Integer.hashCode().

Syntax:

Following is the declaration of hashCode() method:

Parameter:

DataType Parameter Description
int value It is an int value which determines the hash code.

Returns:

Method Returns
hashCode() It returns a hash code value for this object, equal to the primitive int value represented by this Integer object.
hashCode(int value) It returns a hash code value for an int value specified in the method argument.

Exceptions:

InputMismatchException, NumberFormatException

Compatibility Version:

hashCode() Java 1.2 and above
hashCode(int value) Java 1.8 and above

Example 1

Test it Now

Output:

Hash code Value for object is: 155

Example 2

Output:

1.	Enter the desired input value: 4343
Hash code Value for object is: 4343

2.	Enter the desired input value: abcd
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at  myPackage.IntegerHashCodeExample1.main(IntegerHashCodeExample1.java:11)

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "abcd"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.(Integer.java:867)
	at myPackage.IntegerHashCodeExample3.main(IntegerHashCodeExample3.java:5)

Example 4

Test it Now

Output:

Hash code Value for object is: 155

Example 5

Output:

Enter the desired input value: 4569
Hash code Value for object is: 4569






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA