🧱 What is the Object Class in Java?
The Object class is the root class in Java's class hierarchy. Every class in Java, whether
directly or indirectly, inherits from Object. This means that all objects in Java, including
arrays, implement the methods defined in the Object class Oracle Docs.
Key Characteristics:
Universal Inheritance: Every class in Java inherits from Object, making it the ultimate
superclass.
Methods: Provides fundamental methods that are available to all objects, such as
toString(), equals(), hashCode(), and more.
Java Object Class – Common Methods with Examples & Explanations
1️⃣ toString()
Purpose: Returns a string representation of the object.
Why useful: Lets us print objects meaningfully instead of memory address.
class Person {
String name = "Alice";
public class ToStringExample {
public static void main(String[] args) {
Person p = new Person();
[Link]([Link]());
// Output: Person@hashcode (default from Object)
2️⃣ equals()
Purpose: Checks if two objects are equal.
Why useful: Compare object contents instead of references.
class Person {
String name;
Person(String name){ [Link] = name; }
public class EqualsExample {
public static void main(String[] args) {
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
[Link]([Link](p2));
// Output: false (default compares references)
3️⃣ hashCode()
Purpose: Returns an integer hash code for the object.
Why useful: Needed for hash-based collections like HashMap, HashSet.
class Person {
String name = "Alice";
public class HashCodeExample {
public static void main(String[] args) {
Person p = new Person();
[Link]([Link]());
// Output: some integer value
}
4️⃣ getClass()
Purpose: Returns the runtime class of the object.
Why useful: Helps know object type at runtime.
class Person { }
public class GetClassExample {
public static void main(String[] args) {
Person p = new Person();
[Link]([Link]());
// Output: class Person
5️⃣ == (Reference equality)
Purpose: Checks if two references point to the same object.
Why useful: To know if two variables point to exact same object.
class Person { }
public class ReferenceExample {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
[Link](p1 == p2); // false
[Link](p1 == p1); // true
6️⃣ clone()
Purpose: Creates a copy of the object.
Why useful: To duplicate an object without manually copying properties.
class Person implements Cloneable {
String name = "Alice";
protected Object clone() throws CloneNotSupportedException {
return [Link]();
public class CloneExample {
public static void main(String[] args) throws Exception {
Person p1 = new Person();
Person p2 = (Person) [Link]();
[Link](p1 == p2); // false, different objects
7️⃣ finalize()
Purpose: Called by garbage collector before destroying object.
Why useful: To clean up resources before object is removed.
class Person {
protected void finalize() {
[Link]("Finalize called");
public class FinalizeExample {
public static void main(String[] args) {
Person p = new Person();
p = null; // eligible for GC
[Link](); // may call finalize
8️⃣ wait(), notify(), notifyAll()
Purpose: Used in multithreading for communication between threads.
Why useful: Makes threads wait and notify safely.
class Shared {
synchronized void waitThread() throws InterruptedException {
[Link]("Waiting...");
wait(); // waits
[Link]("Resumed!");
synchronized void notifyThread() {
[Link]("Notify called");
notify(); // notifies waiting thread
public class WaitNotifyExample {
public static void main(String[] args) throws InterruptedException {
Shared s = new Shared();
Thread t1 = new Thread(() -> {
try { [Link](); } catch(Exception e) {}
});
[Link]();
[Link](1000);
[Link]();
✅ Summary Table:
Method Purpose Example Usage
toString() Get string representation [Link]()
equals() Compare objects [Link](p2)
hashCode() Get hash code [Link]()
getClass() Get runtime class [Link]()
== Check reference equality p1 == p2
clone() Duplicate object (Person)[Link]()
finalize() Cleanup before GC [Link]() triggers it
wait/notify Thread communication wait() / notify()