What is the object class in Java?



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.

Example

Following example demonstrates the usage of the Object class. Here we are getting the name of the current class using the getClass() method.

Live Demo

import java.util.GregorianCalendar;
public class ObjectDemo {
   public static void main(String[] args) {
      //Create a new ObjectDemo object
      GregorianCalendar cal = new GregorianCalendar();
      
      //Print current time
      System.out.println("" + cal.getTime());
      
      //Print the class of cal
      System.out.println("" + cal.getClass());
      
      //Create a new Integer
      Integer i = new Integer(5);
      
      //Print i
      System.out.println("" + i);
      
      //Print the class of i
      System.out.println("" + i.getClass());
   }
}

Output

Sat Sep 22 00:31:24 EEST 2012
class java.util.GregorianCalendar
5
class java.lang.Integer

Advertisements