Home » Java programming language

Java Object Class int hashCode() method with Example

Java Object class int hashCode() method: Here, we are going to learn about the int hashCode() method of Object class with its syntax and example.
Submitted by Preeti Jain, on June 25, 2019

Object Class int hashCode()

  • This method is available in package java.lang.Object.hashCode().
  • This method is used to return hashcode for the object.

Syntax:

    int hashCode(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Object.

Return value:

The return type of this method is int that means this method returns hashcode of the object and return type is int that means hashcode is in number format.

Java program to demonstrate example of Object Class hashCode() method

import java.lang.Object;

public class ObjectClass {
    public static void main(String[] args) {

        // Create a new object for Integer type
        Integer in = new Integer(10);

        // Display hashcode of Integer class object
        System.out.println("The hashCode of Integer class object in is :" + in .hashCode());

        // Create a new object for String type
        String str = new String("Hello, Welcome in java world");

        // Display hashcode of String class object
        System.out.println("The hashCode of String class object str is :" + str.hashCode());
    }
}

Output

D:\Programs>javac ObjectClass.java

D:\Programs>java ObjectClass
The hashCode of Integer class object in is :10
The hashCode of String class object str is :508604331



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.