Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of Object and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all Java classes. Hence Object class acts as a root of the inheritance hierarchy in any Java Program. Show Using Object Class MethodsThe Object class provides multiple methods which are as follows:
1. toString() methodThe toString() provides a String representation of an object and is used to convert an object to a String. The default toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, it is defined as: // Default behavior of toString() is to print class name, then // @, then unsigned hexadecimal representation of the hash code // of the object public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } It is always recommended to override the toString() method to get our own String representation of Object. For more on override of toString() method refer – Overriding toString() in Java
Student s = new Student(); // Below two statements are equivalent System.out.println(s); System.out.println(s.toString()); 2. hashCode() methodFor every object, JVM generates a unique number which is a hashcode. It returns distinct integers for distinct objects. A common misconception about this method is that the hashCode() method returns the address of the object, which is not correct. It converts the internal address of the object to an integer by using an algorithm. The hashCode() method is native because in Java it is impossible to find the address of an object, so it uses native languages like C/C++ to find the address of the object. Use of hashCode() methodIt returns a hash value that is used to search objects in a collection. JVM(Java Virtual Machine) uses the hashcode method while saving objects into hashing-related data structures like HashSet, HashMap, Hashtable, etc. The main advantage of saving objects based on hash code is that searching becomes easy.
Java
Output : Student@64 Student@64 Note that 4*160 + 6*161 = 100 3. equals(Object obj) methodIt compares the given object to “this” object (the object on which the method is called). It gives a generic way to compare objects for equality. It is recommended to override the equals(Object obj) method to get our own equality condition on Objects. For more on the override of equals(Object obj) method refer – Overriding equals method in Java
4. getClass() methodIt returns the class object of “this” object and is used to get the actual runtime class of the object. It can also be used to get metadata of this class. The returned Class object is the object that is locked by static synchronized methods of the represented class. As it is final so we don’t override it. Java
Output: Class of Object obj is : java.lang.String
5. finalize() methodThis method is called just before an object is garbage collected. It is called the Garbage Collector on an object when the garbage collector determines that there are no more references to the object. We should override finalize() method to dispose of system resources, perform clean-up activities and minimize memory leaks. For example, before destroying the Servlet objects web container, always called finalize method to perform clean-up activities of the session.
Java
Output: 366712642 finalize method called end 6. clone()methodIt returns a new object that is exactly the same as this object. For clone() method refer Clone(). The remaining three methods wait(), notify() notifyAll() are related to Concurrency. Refer to Inter-thread Communication in Java for details. What is it called when an object is created from a class?When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.
Which method gets called when you create an object?Whenever the clone() method is called, it creates a new object, and then gets all the content in the old object copied to it.
|