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 Object is the root of the class hierarchy.
Every class has Object as a superclass.
All objects, including arrays, implement the methods of this class.
The Object class provides multiple methods which are as follows:
- tostring() method.
- hashCode() method.
- equals(Object obj) method.
- finalize() method.
- getClass() method.
- clone() method.
- wait()
- notify()
- notifyAll() methods.
tostring( ) Method:
returns the string representation of this object.
The toString( ) provides a String representation of an object and is used to convert an object to a String.
Whenever we try to print any Object reference, then internally toString( ) method is called.
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.
hashCode( ) Method:
returns the hashcode number for this object.
Override of hashCode() method needs to be done such that for every object we generate a unique number.
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.
equals(Object ob) method:
- It 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.
- It is generally necessary to override the hashCode() method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Methods of Object class:
| The Object class provides many methods. They are as follows: |
| Method | Description |
|---|---|
| public final Class getClass() | returns the Class class object of this object. The Class class can further be used to get the metadata of this class. |
| public int hashCode() | returns the hashcode number for this object. |
| public boolean equals(Object obj) | compares the given object to this object. |
| protected Object clone() throws CloneNotSupportedException | creates and returns the exact copy (clone) of this object. |
| public String toString() | returns the string representation of this object. |
| public final void notify() | wakes up single thread, waiting on this object’s monitor. |
| public final void notifyAll() | wakes up all the threads, waiting on this object’s monitor. |
| public final void wait(long timeout)throws InterruptedException | causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait(long timeout,int nanos)throws InterruptedException | causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait()throws InterruptedException | causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). |
| protected void finalize()throws Throwable | is invoked by the garbage collector before object is being garbage collected. |
Reference :
1.https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
2.https://www.geeksforgeeks.org/object-class-in-java/
3.https://www.javatpoint.com/object-class
Advertisements