Witscale Test Center

13.6 The java.lang.Object class > 13.6.1 The methods defined in Object class


13.6.1 The methods defined in Object class

The key methods in Object class include two methods for object equality (equals() and hashCode()), five methods related to Java threading (wait(), notify() and notifyAll()), one method related to garbage collection (finalize()) and two methods for providing information about the object (toString() and getClass()).

Object equality methods

The equals() method in Object class accepts a object reference as argument. The signature of this method is:

 

public boolean equals(Object obj)

 

This method returns true only when the reference passed as argument points to the current object. In simple words, it returns true when the two references (the this reference and reference passed as argument) denotes the same object. The complete method definition in Object class is as:

 

public boolean equals(Object obj) {

     return (this == obj);

}

 

Thus the equals() method in Object class essentially compares the object references. But the Object class defines the equals() method so that the subclasses could override it to compare their values (state) while checking whether they are equal or not. Thus, the subclasses can specify the criterion of equality in the overridden equals() method.

Another very important method related to object equality is the hashCode() method. We will discuss this method in next chapter. For now, remember that this method is defined in Object class. Hence all Java classes inherit it. The signature of this method is :

 

public native int hashCode();

 

This method returns a integer value (a hashcode value) for the object. The specialty of this method is that if you invoke it more than once on the same object, during the execution of a Java application, it will consistently return the same integer. For example,

 

String test = "Test";

System.out.println("Hashcode for string object :" + test.hashCode());

System.out.println("Doing something...") ;

System.out.println("Hashcode for string object :" + test.hashCode());

 

The output would be ...

 

Hashcode for string object :2603186

Doing something...

Hashcode for string object :2603186

 

Thus hashCode() returns same integer number for any Java object during an execution of a Java program.

 

The relation between equals() method and hashcode() is such that if two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result. If objects of a particular class are to be stored and retrieved from a collection, it is important for that class to override equals() method and hashcode() methods. Since equals() in Object class only compare object references, it returns true only when both object references are pointing to the same object. Hence, this methods needs to be overridden to do equality test based on objects contents.

 

Threading related methods

The Object class provides support for threading through the wait() and notify() methods. We have already learned about these methods (Chapter 12 Threads). Here are the method signatures of five threading related methods:

 

public final native void notify();

public final native void notifyAll();

public final void wait() throws InterruptedException

public final native void wait(long timeout) throws InterruptedException;

public final void wait(long timeout, int nanos) throws InterruptedException

 

Note that all these methods are final, hence they cannot be overridden buy the subclasses. Thus, the threading behavior supported by Object class cannot be altered by subclasses.

Information methods

The two methods getClass() and toString() gives you a generic information about the object on which they are invokes. The signatures of getClass() method is :

 

public final native Class getClass();

 

This method returns the runtime class of an object on which it is invoked. The class is represented as an instance of java.lang.Class.  This method is final and cannot be overridden. Only Java runtime gets to decide what class corresponds to this object.

The another informative method, toString() is often overridden. The one defined in Object class returns a textual representation of object in the following form:

 

<class name>@<hashcode of the object>

 

Most of the Java classes override this method to provide a meaningful textual representation. For instance, the Date class overrides it to provide a textual representation of Date object instead of the cryptic default representation java.util.Date@1df454 which the Object’s toString() would provide.  Most of the subclasses override this method in such a way that the textual representation gives details about the state of the object at the time it is invoked. For this reason, this method is heavily used for debugging purposes, as you are likely to know the guts of object at any given time by invoking this method.

Garbage collection related method

The Object class also defines a finalize() method which is automatically invoked (by JVM) just before the Object is to be garbage collected. The signature of this method is :

 

protected void finalize() throws Throwable

 

The Java classes may override this method to perform any clean up just before the object is erased from the memory.