The Object Class in java

The Object Class

The Object class is the root of all classes in the Java technology programming language. If a class is declared with no extends clause, then the compiler adds implicitly the code extends Object to the declaration;
for example:

public class Employee {

// more code here


is equivalent to:

public class Employee extends Object {

// more code here


This enables you to override several methods inherited from the Object class. The following sections describe two important Object methods.

The equals Method

The == operator performs an equivalent comparison. That is, for any reference values x and y, x==y returns true if and only if x and y refer to the same object. The Object class in the java.lang package has the method public boolean equals(Object obj), which compares two objects for equality. When not overridden, an object’s equals() method returns true only if the two references being compared refer to the same object.

However, the intention of the equals() method is to compare the contents of two objects whenever possible. This is why the method is frequently overridden. For example, the equals() method in String class returns true if and only if the argument is not null and is a String object that represents the same sequence of characters as the String object with which the method is invoked.

Note – You should override the hashCode method whenever you override the equals method. A simple implementation could use a bitwise XOR on the hash codes of the elements tested for equality.

An equalsExample

In this example, the MyDate class has been modified to include an equals method that tests against the year, month, and day attributes.

1 public class MyDate {

2 private int day;

3 private int month;

4 private int year;


6 public MyDate(int day, int month, int year) {

7 = day;

8 this.month = month;

9 this.year = year;

10 }


12 public boolean equals(Object o) {

13 boolean result = false;

14 if ( (o != null) && (o instanceof MyDate) ) {

15 MyDate d = (MyDate) o;

16 if ( (day == && (month == d.month)

17 && (year == d.year) ) {

18 result = true;

19 }

20 }

21 return result;

22 }


24 public int hashCode() {

25 return (day ^ month ^ year);

26 }

27 }

The hashCode method implements a bitwise XOR of the date attributes. This guarantees that hash code for equal MyDate objects have the same value while making it likely that different dates will return different values.

The following program tests two MyDate objects that are not identical, but are equal relative to the year-month-day test.

1 class TestEquals {

2 public static void main(String[] args) {

3 MyDate date1 = new MyDate(14, 3, 1976);

4 MyDate date2 = new MyDate(14, 3, 1976);


6 if ( date1 == date2 ) {

7 System.out.println(“date1 is identical to date2”);

8 } else {

9 System.out.println(“date1 is not identical to date2”);

10 }


12 if ( date1.equals(date2) ) {

13 System.out.println(“date1 is equal to date2”);

14 } else {

15 System.out.println(“date1 is not equal to date2”);

16 }


18 System.out.println(“set date2 = date1;”);

19 date2 = date1;


21 if ( date1 == date2 ) {

22 System.out.println(“date1 is identical to date2”);

23 } else {

24 System.out.println(“date1 is not identical to date2”);

25 }

26 }

27 }

The execution of this test program generates the following output:

date1 is not identical to date2

date1 is equal to date2

set date2 = date1;

date1 is identical to date2

The toString Method

The toString method converts an object to a String representation. It is referenced by the compiler when automatic string conversion takes place.

For example, the System.out.println() call:

Date now = new Date();


is equivalent to:


The Object class defines a default toString method that returns the class name and its reference address (not normally useful). Many classes override toString to provide more useful information. For example, all wrapper classes (introduced later in this module) override toString to provide a string form of the value they represent. Even classes representing items without a string form often implement toString to return object state information for debugging purposes.