Object class in java
The Object class, in the java.lang
package, is the topmost class in java bydefault. In other words, it is the root of all the class hierarchy.
You can use this class as reference of any child class whose type you don’t know. It will clear from the example given below. We have one method say ‘getInt’ which returns int type value. But suppose, you don’t know its return type, in that case you can use the Object class as reference.
public static void main(String[] args) { // use object class to capture the returned value Object b = getInt(2); System.out.println(b); } //method returns int public static int getInt(int a){ System.out.println("it will return integer a "); return a; }
Its methods and descriptions:
Methods | Description |
public boolean equals(Object obj) | Indicates whether some other object is “equal to” this one |
protected Object clone() throws CloneNotSupportedException | Creates and returns a copy of this object. |
protected void finalize() throws Throwable | Called by the garbage collector to release the object which is unused |
public final Class getClass() | Returns the runtime class of an object. |
public int hashCode() | Returns a hash code value for the object. |
public String toString() | Returns a string representation of the object |
public final void notify() | wakes up single thread. |
public final void notifyAll() | wakes up all thread. |
public final void wait() | keep the current thread to wait, until another thread notifies (using notify() or notifyAll() method). |
public final void wait(long timeout) | Keeps the current thread to wait for the specified milliseconds, until another thread notifies (using notify() or notifyAll() method). |
public final void wait(long timeout, int nanos) | Keeps the current thread to wait for the specified miliseconds and nanoseconds, until another thread notifies (using notify() or notifyAll() method). |
1 Response
[…] will not throw any error but a default constructor of Object class will be called. Every class extends Object class by default which is a parent class for all the […]