Last Updated:

Hashcode() and Equals() methods in Java

Hashcode() and Equals() in Java programming is a common topic that beginners encounter. Here you need to understand what each method means and how you want to write code with them. Just in this material we will consider what a novice Java programmer needs to know in order to work in the future without any special misunderstandings and difficulties with the Hashcode() and Equals() methods.

But first, before you deal directly with the very work of these parts of the code, you need to understand what Hashcode() is. In simple terms, this is a component called the result of the method, to the input parameter of which the object is passed. Or you can call it something else - a bit string of fixed length, obtained from an array of arbitrary length.

How do I write Hashcode() and Equals()?

In order to understand how to write Hashcode() and Equals() in the Java language, you need to use many examples for each situation. But first, you need this simple, explaining how Hashcode() works:

publicclassMain {
public static void main(String[] args) {
Object object = new Object();
int hCode;
hCode = object.hashCode();

This example is the most basic and is required solely for users to understand the principle of the command. After executing this code, a 10-digit number will be displayed in the result window, which will be a bit string of fixed length. The limit here is indicated using int, which is also required to know for beginners, because in the future there will be similar structures with which there should be no misunderstandings.

Since an array of arbitrary length is an object, in this example it is also expressed in a certain way. Here, object is designated as such, and a whole line of code is responsible for it.
Thanks to the example discussed above, the user can deal with the codes involving Hashcode() without any problems.

But it is also necessary to study Equals(), because it also has a lot of important material. Before you start writing code examples again, you need to explain one detail - when the new operator is created, each time a new object will appear in memory. And to illustrate all this, you need to create a new class in the code:

public class BlackBox {
int varA;
int varB;

BlackBox(int varA, int varB){
this.varA = varA;
this.varB = varB;

However, this may not be enough in the code that the user creates. In order to continue the analysis of the example, you need to use the demo class. Here's what it would look like in addition to the class description you already have:

public class DemoBlackBox {
public static void main(String[] args) {
BlackBox object1 = new BlackBox(5, 10);
BlackBox object2 = new BlackBox(5, 10);

When 2 objects are created in memory, each with its own properties, you will have to use a tool to compare both. Although a person will understand that the properties of both elements are equal, but you also need to make sure of this with the help of a command. It looks like this:


There can be 2 outcomes here. Either the program will output the answer "Booleantrue" if the values are equivalent, or "False" if something is not equal. Here's another example with Equals() and Hashcode():

object1.hashCode() == object2.hashCode()

In both cases, it should be "True". And now that the main material with both commands has been analyzed, you should start overriding the Equals() and Hashcode() values.

Overriding Equals() and Hashcode() in Java

Sometimes you need to override the Equals() method. Java recommends that you observe several important points when doing this:

  • The object must be equal to itself;
  • A.equals(b) should return true, and b.equals(a) should also return true(for example). Same with 3 "equals";
  • A second call to equals() should return a single unchanged value until some of the properties are changed;
  • The object must be nullified. If it is equal to this value, a.equals(null) will return false.

Here's one example of overriding the Equals() method:

public class Person {
private int id;
private String firstName;
private String lastName;

public int getId() { return id; }
public void setId(int id) { = id;}

public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public boolean equals(Object obj) {
if (obj == this) {
return true;
if (obj == null || obj.getClass() != this.getClass()) {
return false;


Person guest = (Person) obj;
return id ==
&& (firstName == guest.firstName
|| (firstName != null &&firstName.equals(guest.getFirstName()))) && (lastName == guest.lastName
|| (lastName != null && lastName .equals(guest.getLastName())
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + id; result = prime * result +
((lastName == null) ? 0 : lastName.hashCode()); return result;


What do I need to remember about Equals() and Hashcode()?


It is also necessary to remember what may be most important in the process of writing code. Equals() and Hashcode() have special features that need to be remembered. Sometimes they can be very useful – depending on the situation.

It is important to remember that if the hash codes are different, then the input objects will differ from each other - this is guaranteed. If the hash codes are equal, then the input objects have no equality with each other.

The latter phenomenon can be called a collision – when different objects have the same hash code value. The probability of such a occurrence, as a rule, can vary greatly and this can be achieved depending on the Hashcode() generation algorithm used.

About Equals() you need to know only one thing and the most important thing. This is the purpose of the code particle. It is needed solely in order to identify equality or inequality between objects. Equals() has no other purpose.