+ 1

I don't understand the hashcode() concept and equals concept here. Can someone explain me?

The code is arcane to me. Please, someone explain it to me.

24th Dec 2021, 9:56 AM
Ashutosh Das
Ashutosh Das - avatar
2 Answers
+ 2
Maybe you will show us this absurd code?
24th Dec 2021, 11:19 AM
Coding Cat
Coding Cat - avatar
0
Let us take a Student class as an example. class Student { String name; Student(String name){ this.name = name; } } Now let us create 2 objects. Student s1 = new Student("Ashutosh"); Student s2 = new Student("Ashutosh"); We have 2 ways to compare these objects, let us see them one by one. s1 == s2 returns false because both of them are created in two different memory locations. s1.equals(s2) also returns false since even the equals() checks for reference instead of the content for objects. Now in order to make both the objects equal, we need to override the equals() method. Always remember that if two objects are equal than the hashcode generated must also be the same but it is not so, that is the reason we override the hashcode() as well. Hope this clears some of your doubt.
24th Dec 2021, 5:22 PM
Avinesh
Avinesh - avatar