Java Object hashCode()

Java object hashcode

What is a Object in Java?

Java is an object-oriented programming language, which means that it is based on the concepts of classes and objects. The properties of an object are represented by its fields or instance variables, and the behavior of an object is represented by its methods.

In Java, an object is an instance of a class. A class is a blueprint or template that defines the properties and methods of an object. An object is created from a class and can be used to access the properties and methods of that class.

  • An object in Java is created using the new keyword, followed by the class name and constructor.

To know more about Object toString in java read the complete article.

Object tostring() method.

In Java, the hashCode() method is a method that is defined in the Object class, which is the parent class of all classes in Java. It returns an integer value that represents the unique hash code of an object. The hash code is a unique identifier for an object, and it is used by certain collection classes, such as HashMap and HashSet, to efficiently store and retrieve objects.

  • The hashCode() method is based on the memory address of the object, but it can be overridden by subclasses to provide a more meaningful hash code that is based on the state of the object. For example, if you have a class called Person with fields for a name and age, you could override the hashCode() method to return a hash code that is based on the name and age of the person.

Syntax:

object.hashCode()

Parameters:

The hashCode() method does not take any parameters.

Let’s look at a object-related Java program where the Java object hashcode Method is used to perform an operation on the given string.

Example: Java Object hashcode() Method

Run

class Animal {
    private String species;
    private int weight;
    private int age;

    public Animal(String species, int weight, int age) {
        this.species = species;
        this.weight = weight;
        this.age = age;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + species.hashCode();
        result = 31 * result + weight;
        result = 31 * result + age;
        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Animal("Lion", 200, 12);
        Animal animal2 = new Animal("Elephant", 600, 20);

        System.out.println("Hashcode for animal1: " + animal1.hashCode());
        System.out.println("Hashcode for animal2: " + animal2.hashCode());
    }
}

Output

Hashcode for animal1: -2018256945
Hashcode for animal2: 1011192316

Example 2 : Java object hashcode() Method

Run

class Car {
    private String make;
    private String model;
    private int year;

    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + make.hashCode();
        result = 31 * result + model.hashCode();
        result = 31 * result + year;
        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Honda", "Civic", 2020);
        Car car2 = new Car("Toyota", "Corolla", 2022);

        System.out.println("Hashcode for car1: " + car1.hashCode());
        System.out.println("Hashcode for car2: " + car2.hashCode());
    }
}

Output

Hashcode for car1: 482534285
Hashcode for car2: -1021003685

Example 3: how to use the object hashcode property:

Run

class Person {
   private String name;
   private int age;

   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }

   @Override
   public int hashCode() {
      int result = 17;
      result = 31 * result + name.hashCode();
      result = 31 * result + age;
      return result;
   }
}

public class Main {
   public static void main(String[] args) {
      Person person1 = new Person("John", 30);
      Person person2 = new Person("Jane", 25);

      System.out.println("Hashcode for person1: " + person1.hashCode());
      System.out.println("Hashcode for person2: " + person2.hashCode());
   }
}

Output

Hashcode for person1: 71767076
Hashcode for person2: 71355484

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription