Introduction
In Java, the Object
class is the root class for all classes. Every class in Java is implicitly or explicitly a subclass of Object
. It provides some common methods that can be used by all objects in Java.
Common Methods in Object Class
toString()
Method:
- The
toString()
method returns a string representation of the object. - The default implementation in the
Object
class returns a string consisting of the class name followed by the “@” character and the object’s hashcode.
public class ExampleObject {
public static void main(String[] args) {
ExampleObject obj = new ExampleObject();
System.out.println(obj.toString()); // Prints something like "ExampleObject@<hashcode>"
}
}
equals(Object obj)
Method:
- The
equals()
method is used to compare the equality of two objects. - By default, it compares the memory addresses of the two objects. However, it is often overridden in user-defined classes to compare the contents.
public class ExampleObject {
public static void main(String[] args) {
ExampleObject obj1 = new ExampleObject();
ExampleObject obj2 = new ExampleObject();
System.out.println(obj1.equals(obj2)); // Prints false
}
}
hashCode()
Method:
- The
hashCode()
method returns a hash code value for the object. - This method is often overridden in user-defined classes for better hash code computation.
public class ExampleObject {
public static void main(String[] args) {
ExampleObject obj = new ExampleObject();
System.out.println(obj.hashCode());
}
}
getClass()
Method:
- The
getClass()
method returns the runtime class of an object.
public class ExampleObject {
public static void main(String[] args) {
ExampleObject obj = new ExampleObject();
Class<? extends ExampleObject> clazz = obj.getClass();
System.out.println(clazz.getName()); // Prints "com.example.ExampleObject"
}
}
Overriding toString()
, equals()
, and hashCode()
It’s common to override these methods in your custom classes to provide meaningful implementations for your objects. Here’s an example:
public class Person {
private String name;
private int age;
// Constructor, getters, and setters...
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public static void main(String[] args) {
Person person1 = new Person("John", 30);
Person person2 = new Person("John", 30);
System.out.println(person1.equals(person2)); // Prints true
System.out.println(person1.hashCode() == person2.hashCode()); // Prints true
}
}
Conclusion
Understanding the Object
class and its methods is essential when working with Java, as it provides a foundation for object-oriented programming in the language. By overriding methods like toString()
, equals()
, and hashCode()
, you can customize the behavior of your classes to suit your specific needs.