Table of Contents
Introduction
The Object class in Java is present in the java.lang.package. The Object class can be thought of as a mother class, as every other class is derived from the Object class. A class is directly inherited from the object class if it is not asked to extend from any other class by default. Even if the class does extend to another class, it still remains a class that indirectly derives from the Object class. This follows the methods of the Object class can be derived by all classes for any purpose. Therefore, the Object class acts as a root of inheritance hierarch in a Java Program.
Using Object Class Methods
Some of the Most Used Methods of the Object class are as follows:
toString ()
As the name suggests, this method will provide you with the String representation of an object. In simpler words, this method will convert the object passed as a parameter to a String. The default toString () method for the class Object returns the following:
- The name of the class whose instance is the object in question
- The ‘@’ sign character,
- The unsigned hexadecimal illustration of the hash code of the object.
The following code snippet better explains this representation:
//Default behavior of toString () is to print the class name. after that @, and thereafter the unsigned hexadecimal illustration of the hash code of the object.
public String toString () { return getClass (). getName () + “@” + Intege.toHexString (hashCode () ); } Java convention dictates that this toString () method should be overridden inside the class where it is being used. This will allow users to get their own String representation of the Object that is passed as parameter in this method. The toString () method is invoked by default by the Java Virtual Machine whenever an Object is printed out to the console. Student s = new Student (); //Below two statements are equivalent System.out.println (s); System.out.println (s.toString ());
Hash Code ()
Whenever a class is instantiated, and an object is created, the Java Virtual Machine creates a unique number for that object which is called the hash code. The hash code is a unique integer number for every object. A lot of people think that only invocation of the hashCode () method, it actually returns the address of that particular object. This is not the case at all. With the help of an algorithm, the JVM converts the address of the object into an integer which is returned from this method. In Java, it is not computationally feasible to find the exact address of each object. Therefore, this hashCode () method derives from the implementation of native languages like C/C++ to find and return the address of an object.
Use of the hashCode () Method: When this method is called, an integer value is returned which is called the hash value of the object. This hash value is used to look up the address of the object in a collection. Java harbors several collections like HashSet, HashMap, HashTable, etc. When saving objects inside these collections, Java makes use of the hash code for the unique identification of the same. In this manner, it takes less time to go through a collection and search for an object that is part of it.
Java convention again mandates that this hashCode () method be overridden in the class that you are dealing with such that the hash code of the objects can be easily retrieved. As an example, if we have a class of students, we can return the roll number of the student depending on the hash code as follows:
//Java program to demonstrate the working of the hashCode () and toString () public class Student { static int last_roll = 100; int roll_no; //Constructor Student () { roll_no = last+roll; last_roll++; } //Overriding hashCode () @Override public int hashCode () { return roll_no; } //Driver Code public static void main (String args []) { Student s = new Student (); //Below two statements are equivalent System.out.println (s); System.out.println (s.toString () ); } }
OUTPUT
Student @ 64
Student @ 64
Note: 4*16^0 + 6*16^1 = 100
2. equals (Object obj): As the name suggests, this method compares the given object to the object that is passed as a parameter to the method. The equals () method is a very generic way to compare the equality of two objects, and it is always recommended to override this method in your class such that you can compare the two objects based on the equality factor of your own choice and conditions.
Note: It is important that you override this method whenever you are overriding the hashCode () method. This is done to maintain the general contract for the hashCode method that says objects that are equal should also have equal hashcodes.
getClass ()
This method returns the class object of the object that is passed as a parameter. It is also commonly used to get information about the runtime class of the object. It can also be used to get the metadata of this class. The returned Class object is the object that is locked by static synchronized methods of the illustrated class. It is not overridden due to its final nature of it.
//Java Program to demonstrate the working of getClass () public class Test { public static void main (String args []) { Object obj = new String (“ Hello World ”); Class c = obj.getClass () ; System.out.println ( “ The Class of Object obj is: ” + c.getName () ); } }
OUTPUT
The Class of Object obj is java.lang.String
Once the .class file is loaded in the Java Virtual Machine, it creates a java.lang.Class object in the heap area. This object provides us with class-level information and is commonly used in the Reflection phenomenon.
finalize () method
The finalize method is a method called by the Garbage collector right before an object loses its usage and is thrown as garbage. This happens when the object does not have any more references to it. This method should also be overridden such that system resources can be disposed of, clean-up activities can be performed, and leaks in the memory can be reduced. As an example, you can think of the clean-up procedure of the Servlet objects web container. As a part of this process, the finalize method is always called.
Even if a particular object is eligible for garbage collection more than once, the finalize () method can only be called one on that object.
//Java Program to demonstrate the working of finalize () public class Test { public static void main (String args []) { Test t = new Test (); System.out.println (t.hashCode () ); t = null; //calling garbage collector System.gc(); System.out.println (“ end ”); } @Override protected void finalize () { System.out.println (“ finalize method called ”); } }
OUTPUT
366712642
finalize method called
end
clone()
Cloning is the process of replicating. In java, the clone () method creates a new object that is the exact replica of the object it is cloning.
The wait (), notify (), and notifyAll () methods are also Java Methods that are related to Concurrency.
0 Comments