Flexible nature of java.lang.object


The java.lang.Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Class Declaration

Following is the declaration for java.lang.Object class −

public class Object

Class constructors

Sr.No.Constructor & Description
1Object()
This is the Single Constructor.

Class methods

Sr.No.Method & Description
1protected Object clone()This method creates and returns a copy of this object.
2boolean equals(Object obj)This method indicates whether some other object is "equal to" this one.
3protected void finalize()This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
4Class<?> getClass()This method returns the runtime class of this Object.
5int hashCode()This method returns a hash code value for the object.
6void notify()This method wakes up a single thread that is waiting on this object's monitor.
7void notifyAll()This method wakes up all threads that are waiting on this object's monitor.
8String toString()This method returns a string representation of the object.
9void wait()This method causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
10void wait(long timeout)This method causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
11void wait(long timeout, int nanos)This method causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

Object class being super class of each and every class in Java

  • can be used to assign any type of object.

  • can be used to pass as an argument where function can accept any type of object.

  • can be used to provide default functionality of toString(), clone() method.

Example Final

import java.io.File;
import java.text.NumberFormat;

public class Tester {
   public static void main(String[] args) {

      Object object;

      object = 'A';
      printObjectType(object);

      object = "A";
      printObjectType(object);

      object = 1;
      printObjectType(object);

      object = 1.2;
      printObjectType(object);

      object = false;
      printObjectType(object);
   }
   private static void printObjectType(Object object) {
      System.out.println(object.getClass().getName());
   }
}

Output

java.lang.Character
java.lang.String
java.lang.Integer
java.lang.Double
java.lang.Boolean

Updated on: 21-Jun-2020

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements