The Object class, present in the java.lang package, is the root class of the Java class hierarchy. Every class in Java implicitly or explicitly inherits from the Object class. It provides basic methods that all Java objects inherit and can use.
Here is a list of methods provided by the Object class
lets explore each method of Object class
public class Car { private String brand; private String model; public Car(String brand, String model) { this.brand = brand; this.model = model; } public static void main(String[] args) { Car car = new Car("Toyota", "Camry"); Class<?> carClass = car.getClass(); System.out.println(carClass.getName()); // Output: Car }}carxxxxxxxxxxpublic class Car implements Cloneable { private String brand; private String model; public Car(String brand, String model) { this.brand = brand; this.model = model; } protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) { try { Car car1 = new Car("Toyota", "Camry"); Car car2 = (Car) car1.clone(); System.out.println(car1 != car2); // true, different objects System.out.println(car1.getClass() == car2.getClass()); // true System.out.println(car1.equals(car2)); // false, by default equals checks reference } catch (CloneNotSupportedException e) { e.printStackTrace(); } }}hashCode() is used to find the bucket where the object should be placed. The default implementation provided by Object class returns a distinct integer for each object.Usage in Collections:
HashMap, HashSet, and other hash-based collections, hashCode() is used to find the bucket where the object should be placed.equals() method is used to compare the objects in the bucket to ensure uniqueness.xxxxxxxxxximport java.util.*;class Student{ int id; String name; String age; public Student(int id, String name, String age) { this.id = id; this.name = name; this.age = age; } public int hashCode() { System.out.println("this.id: " + this.id + ", hashCode=> " + Objects.hash(this.id)); return Objects.hash(this.id); } public boolean equals(Object obj) { System.out.println("this: " + this); // `this`: s1 ( and hashCode() method will be called for s1 object hashCode calculation ) System.out.println("obj: " + obj); // `obj`: s2 ( and hashCode() method will be called for s2 object hashCode calculation ) // this (current object: s1) // s1.equals(s2) and s2: object Integer id1 = this.id; String name1 = this.name; String age1 = this.age; System.out.println("this=> "+ "id1: " + this.id + ", name1: " + this.name + ", age1: " + this.age); // s2 is obj Student s = (Student) obj; Integer id2 = s.id; String name2 = s.name; String age2 = s.age; System.out.println("obj=> " + "id2: " + s.id + ", name2: " + s.name + ", age2: " + s.age); if(this == obj) { System.out.println("this == obj"); // for this we need call like s1.equals(s1) (or) s2.equals(s2) return true; } if (obj == null || getClass() != obj.getClass()) { return false; } return id == s.id; }}public class HashCodeAndEquals{ public static void main(String[] args) { Student s1 = new Student(3, "S1", "25"); Student s2 = new Student(3, "S2", "28"); Student s3 = new Student(4, "S2", "28"); System.out.println("s1.equals(s2): " + s1.equals(s2)); // s1 is `this` and s2 is `obj` System.out.println("s2.equals(s3) " + s2.equals(s3)); // s2 is `this` and s3 is `obj` System.out.println("s1 hash: " + s1.hashCode()); System.out.println("s2 hash: " + s2.hashCode()); }}this.id: 3, hashCode=> 34
this: Student@22
this.id: 3, hashCode=> 34
obj: Student@22
this=> id1: 3, name1: S1, age1: 25
obj=> id2: 3, name2: S2, age2: 28
s1.equals(s2): true
this.id: 3, hashCode=> 34
this: Student@22
this.id: 4, hashCode=> 35
obj: Student@23
this=> id1: 3, name1: S2, age1: 28
obj=> id2: 4, name2: S2, age2: 28
s2.equals(s3) false
this.id: 3, hashCode=> 34
s1 hash: 34
this.id: 3, hashCode=> 34
s2 hash: 34Overriding hashCode():
hashCode() method is overridden to compute the hash code based on the id field.Objects.hash(this.id) is used to compute the hash code.id and the computed hash code for debugging purposes.Overriding equals():
equals() method is overridden to compare Student objects based on the id field.obj is the same as this using obj == this.obj is null or not an instance of the Student class.id fields of the two Student objects to determine equality.NOTE:
xxxxxxxxxximport java.util.*;public class Car { private String brand; private String model; public Car(String brand, String model) { this.brand = brand; this.model = model; } public boolean equals(Object obj) { System.out.println("this.class: " + getClass()); System.out.println("obj.class: " + obj.getClass()); if (this == obj) return true; // this refers to current class object if (obj == null || getClass() != obj.getClass()) return false; Car car = (Car) obj; return brand.equals(car.brand) && model.equals(car.model); } public static void main(String[] args) { Car car1 = new Car("Toyota", "Camry"); Car car2 = new Car("Toyota", "Camry"); System.out.println(car1.equals(car2)); // true }}this.class: class Car
obj.class: class Car
trueNOTE:
In String class .equals() method is overriden for content comparision, but not for reference comparison.