Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 5

The root of all classes

  • Print
  • Feedback

Page 5 of 6

Equality

Comparing the contents of two variables to determine if they are equal, and comparing two objects to determine if they are equal are two different tasks. When it comes to comparing two variables' contents, developers often use the equality operator (==), which returns a Boolean true value if the contents are the same. Otherwise, false returns. For example, suppose you specify the following code fragment:

int i = 10;
int j = 10;
int k = 13;
System.out.println ("i == j: " + (i == j));
System.out.println ("i == k: " + (i == k));


It will probably come as no surprise that the code fragment prints:

i == j: true
i == k: false


When comparing two variables of the same primitive type (such as integer), equality comparison is straightforward. However, many developers make the mistake of using == to compare objects. For example, a developer might specify the following code fragment:

Employee e1 = new Employee ("John Doe");
Employee e2 = new Employee ("John Doe");
Employee e3 = new Employee ("Jane Doe");
System.out.println ("e1 == e2: " + (e1 == e2));
System.out.println ("e1 == e3: " + (e1 == e3));


When the preceding code fragment runs, you might expect to see the following output:

e1 == e2: true
e1 == e3: false


However, you see:

e1 == e2: false
e1 == e3: false


What's going on? When you compare two variables of the same reference type with the equality operator, you compare the references stored in those variables; you don't compare the objects' contents. The aforementioned code fragment creates three separate Employee objects, and each object has its own distinct reference. As a result, == returns false when comparisons are made. If you want to compare two objects, you must take advantage of Object's equals() method. That method has the following signature:

public boolean equals (Object o)


The idea is to compare the current object with the object whose reference is passed in parameter o. To see how you override equals() to compare objects, check out Listing 10:

Listing 10. EqualityDemo.java

// EqualityDemo.java
class Employee1
{
   String name;
   double salary;
}
class Employee2
{
   String name;
   double salary;
   public boolean equals (Object o)
   {
      if (!(o instanceof Employee2))
          return false;
      Employee2 e = (Employee2) o;
      return (e.name.equals (name) && e.salary == salary);
   }
}
class EqualityDemo
{
   public static void main (String [] args)
   {
      int i = 15;
      int j = 15;
      System.out.println ("i == j: " + (i == j));
      Employee1 e11 = new Employee1 ();
      e11.name = "John Doe";
      e11.salary = 50000.0;
      Employee1 e12 = new Employee1 ();
      e12.name = "John Doe";
      e12.salary = 50000.0;
      System.out.println ("e11 == e12: " + (e11 == e12));
      System.out.println ("e11.equals (e12): " + e11.equals (e12));
      Employee2 e21 = new Employee2 ();
      e21.name = "John Doe";
      e21.salary = 50000.0;
      Employee2 e22 = new Employee2 ();
      e22.name = "John Doe";
      e22.salary = 50000.0;
      System.out.println ("e21 == e22: " + (e21 == e22));
      System.out.println ("e21.equals (e22): " + e21.equals (e22));
   }
}


EqualityDemo presents three classes: Employee1, Employee2, and EqualityDemo. The Employee1 and Employee2 classes are nearly identical, except that Employee2 overrides Object's equals() method.

Employee2's equals() method begins with code that checks o's actual type. If that parameter does not match Employee2, equals() returns false. (Think of the type-checking code as a safeguard for situations such as Employee e = new Employee ("Jack"); boolean b = e.equals ("1.23");. What does it mean to compare an Employee object with a String object?) Employee2 next specifies what it means for two Employee2 objects to be the same. Basically, their name fields must be equal, as do their salary fields. If you run EqualityDemo, you receive the following output:

  • Print
  • Feedback

Resources