Java Object Class

Introduction

The Object class in Java is the root of the class hierarchy. Every class in Java directly or indirectly inherits from Object. It provides basic methods that are fundamental to all objects.

Table of Contents

  1. What is Object?
  2. Common Methods
  3. Examples of Using Object Methods
  4. Conclusion

1. What is Object?

The Object class is the superclass of all classes in Java. It provides a set of methods that are common to all objects.

2. Common Methods

  • equals(Object obj): Compares this object to the specified object.
  • hashCode(): Returns a hash code value for the object.
  • toString(): Returns a string representation of the object.
  • getClass(): Returns the runtime class of the object.
  • clone(): Creates and returns a copy of the object (protected).
  • finalize(): Called by the garbage collector before the object is destroyed (deprecated).
  • notify(): Wakes up a single thread waiting on this object’s monitor.
  • notifyAll(): Wakes up all threads waiting on this object’s monitor.
  • wait(): Causes the current thread to wait until another thread invokes notify() or notifyAll().
  • wait(long timeout): Causes the current thread to wait until either another thread notifies or the specified timeout elapses.
  • wait(long timeout, int nanos): Causes the current thread to wait until another thread notifies, or the specified timeout elapses with nanosecond precision.

3. Examples of Using Object Methods

Example 1: Using equals() and hashCode()

This example demonstrates comparing two objects using equals() and retrieving their hash codes.

public class ObjectEqualsHashCodeExample { public static void main(String[] args) { Object obj1 = new Object(); Object obj2 = new Object(); System.out.println("obj1.equals(obj2): " + obj1.equals(obj2)); System.out.println("obj1.hashCode(): " + obj1.hashCode()); System.out.println("obj2.hashCode(): " + obj2.hashCode()); } } 

Output:

obj1.equals(obj2): false obj1.hashCode(): 2124308362 obj2.hashCode(): 146589023 

Example 2: Using toString()

This example shows how to get a string representation of an object using toString().

public class ObjectToStringExample { public static void main(String[] args) { Object obj = new Object(); System.out.println("obj.toString(): " + obj.toString()); } } 

Output:

obj.toString(): java.lang.Object@2b2fa4f7 

Example 3: Using getClass()

In this example, we use getClass() to obtain the runtime class of an object.

public class ObjectGetClassExample { public static void main(String[] args) { Object obj = new Object(); System.out.println("Class of obj: " + obj.getClass().getName()); } } 

Output:

Class of obj: java.lang.Object 

Example 4: Using wait() and notify()

This example demonstrates thread synchronization using wait() and notify().

public class ObjectWaitNotifyExample { public static void main(String[] args) { final Object lock = new Object(); Thread waitingThread = new Thread(() -> { synchronized (lock) { try { System.out.println("Waiting thread is waiting..."); lock.wait(); System.out.println("Waiting thread resumed."); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread notifyingThread = new Thread(() -> { synchronized (lock) { System.out.println("Notifying thread is notifying..."); lock.notify(); } }); waitingThread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } notifyingThread.start(); } } 

Output:

Waiting thread is waiting... Notifying thread is notifying... Waiting thread resumed. 

4. Conclusion

The Object class in Java is fundamental, providing essential methods that every Java object inherits. Understanding these methods is crucial for effective Java programming, as they form the basis for many object interactions and behaviors.

Leave a Comment

Scroll to Top