how2examples.com
Home - Introduction to Java Programming

The Java Object Class

The java.lang.Object class is the parent class of all other Java classes. The methods of Object are inherited by every class. Subclasses can override the default behaviour of the Object methods to provide class-specific functionality. Common methods to override are toString(), equals(Object) and hashCode().

The public String toString() Method

The toString() method returns a string representation of the object. The version of toString() provided by Object simply returns the object's class name and hash code. Subclasses can override the toString() method to provide a more informative description of an object. The toString() method is used during string-concatenation using the + operator. Providing a string representation of an object's state is useful for debugging.

The public boolean equals(Object obj) Method

The equals(Object) method is used to compare an object to another. The default implementation of equals(Object) only returns true when the specified parameter refers to the exact same object as the object being invoked. The default implementation of equals(Object) is the same as doing a == "shallow" comparison and is often considered too strict. Rather than checking that two references refer to the same instance it is often more useful to check if two objects represent the same thing. Subclasses can overide equals(Object) method to do a comparison based on the relevant instance variables of the two objects.

When you override the equals(Object) method you must conform to the following rules:

The public int hashCode() Method

The hashCode() method returns an int to represent the object. The hashCode() method is required by collections that use hash-based storage (e.g. java.util.HashMap and java.util.HashSet). You must override hashCode() in every class that you override equals(Object) in order for your class to function correctly with hash-based collections. Repeated invocations of hashCode() on an object should return the same integer value provided no information used in comparisons on the object is modified. Implementations of hashCode() must return the same integer results for objects that are considered equal by the equals(Object) method. It can improve performance of hash-based collections if hashCode() returns distinct integer results for objects that are not considered equal by the equals(Object) method - but this is not required.

Contents of ObjectExample.java:
public class ObjectExample {
   public static void main(String[] args) {
      A a1 = new A(1, "a");
      A a2 = new A(2, "a");
      A a3 = new A(1, "b");
      A a4 = new A(1, "a");
      Object o = new Object();
      System.out.println("toString(): "+ a1 + a2 + a3 + a4);
      System.out.println(a1.equals(a2) + " " + a2.equals(a1));
      System.out.println(a1.equals(a3) + " " + a3.equals(a1));
      System.out.println(a1.equals(a4) + " " + a4.equals(a1));
      System.out.println(a1.equals(a1) + " " + a1.equals(a1));
      System.out.println(a1.equals(o) + " " + o.equals(a1));
   }
}

class A {
   final int i;
   final String s;
   
   A(int i, String s) {
      this.i = i;
      this.s = s;
   }
   
   @Override
   public boolean equals(Object o) {
      if (o instanceof A) {
         A a = (A)o;
         return i == a.i && s.equals(a.s);
      } else {
         return false;
      }
   }
   
   @Override
   public int hashCode() {
      return i * s.hashCode();
   }
   
   @Override
   public String toString() {
      return "[" + i + " " + s + "]";
   }
}
Command to compile ObjectExample.java:
javac ObjectExample.java
Command to run ObjectExample:
java ObjectExample
Output from running ObjectExample:
toString(): [1 a][2 a][1 b][1 a]
false false
false false
true true
true true
false false

The protected void finalize() Method

The finalize() method is called on an object when the garbage collector identifies it as being eligible for garbage collection (i.e. there are no references to the object). This method may be overriden to perform some "tidying-up" (e.g. disposing of system resources) when it is known the object is no longer required. In practice this method is rarely overriden. Reasons to avoid overriding finalize() include:

The public final Class<?> getClass() Method

Returns the Class that represents the class of the object. As this method is final it can not be overriden by subclasses.

Contents of GetClassExample.java:
public class GetClassExample {
   public static void main(String[] args) {
      System.out.println(new Object().getClass());
      System.out.println("hello world".getClass());
   }
}
Command to compile GetClassExample.java:
javac GetClassExample.java
Command to run GetClassExample:
java GetClassExample
Output from running GetClassExample:
class java.lang.Object
class java.lang.String

The protected Object clone() Method

Creates and returns a copy of the object. The clone() method will throw a CloneNotSupportedException when called on an object that does not implement Cloneable. Although used by various classes within the JDK, there are reasons why you might be reluctant to use the mechanism clone() supports:

The wait(), notify() and notifyAll() Methods

The wait(), notify() and notifyAll() methods can be used to control concurrent access problems and to communicate between threads.


See Also