Object class is the root of the class hierarchy. Every class has Object as a super class. All objects, including arrays, implement the methods of this class.
In this tutorial you can learn how to use Object class.
/* Save with file name ObjectExample1.java */ public class ObjectExample1 { public static void main(String args[]) { //Object CLASS DECLARATION Object obj; //CREATE INSTANCE FOR Object CLASS obj = new Object(); //Object CLASS OUTPUT //NOTHING TO WORRRY THERE IS NO VALUE //IT PRINTS SOME CODE System.out.println("Object value : " + obj); } }
/* Object Class Example with String Class */ /* Save with file name ObjectExample2.java */ public class ObjectExample2 { public static void main(String args[]) { //String CLASS DECLARATION String s; //CREATE INSTANCE FOR String CLASS s = new String("Huda Tutorials"); //Object CLASS DECLARATION Object obj; //TYPECAST String CLASS INSTANCE TO //Object CLASS INSTANCE obj = (Object) s; //Object CLASS OUTPUT System.out.println("Object value : " + obj); //IT RETURNS RUNTIME CLASS OF THIS OBJECT //java.lang.String System.out.println("Object Class : " + obj.getClass()); } }