Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Comparing Object Values and Classes

In addition to casting, you often will perform three other common tasks that involve objects:

  • Comparing objects
  • Finding out the class of any given object
  • Testing whether an object is an instance of a given class

Comparing Objects

Yesterday, you learned about operators for comparing values—equal, not equal, less than, and so on. Most of these operators work only on primitive types, not on objects. If you try to use other values as operands, the Java compiler produces errors.

The exceptions to this rule are the == operator for equality and the != operator for inequality. When applied to objects, these operators don’t do what you might first expect. Instead of checking whether one object has the same value as the other, they determine whether both sides of the operator refer to the same object.

To compare objects of a class and have meaningful results, you must implement special methods in your class and call those methods.

A good example of this is the String class. It is possible to have two different String objects that represent the same text. If you were to employ the == operator to compare these objects, however, they would be considered unequal. Although their contents match, they are not the same object.

To see whether two String objects have matching values, an equals() method of the class is used. The method tests each character in the string and returns true if the two strings have the same value. The EqualsTester application shown in Listing 3.5 illustrates this. Create the application with NetBeans in the com.java21days package and save the file, either by choosing File, Save or clicking the Save All toolbar button.

LISTING 3.5 The Full Text of EqualsTester.java

 1: package com.java21days;
 3: class EqualsTester {
 4:     public static void main(String[] arguments) {
 5:         String str1, str2;
 6:         str1 = "Boy, that escalated quickly.";
 7:         str2 = str1;
 9:         System.out.println("String1: " + str1);
10:         System.out.println("String2: " + str2);
11:         System.out.println("Same object? " + (str1 == str2));
13:         str2 = new String(str1);
15:         System.out.println("String1: " + str1);
16:         System.out.println("String2: " + str2);
17:         System.out.println("Same object? " + (str1 == str2));
18:         System.out.println("Same value? " + str1.equals(str2));
19:     }
20: }

The program’s output appears in Figure 3.6.


FIGURE 3.6 Calling String methods to learn more about that string.

The first part of this program declares two variables (str1 and str2), assigns the literal “Boy, that escalated quickly.” to str1, and then assigns that value to str2 (lines 5–7). As you learned earlier, str1 and str2 now point to the same object, and the equality test at line 11 proves that.

In the second part of this program, you create a new String object with the same value as str1 and assign str2 to that new String object.

Now you have two different string objects in str1 and str2, both with the same value. Testing them to see whether they’re the same object by using the == operator returns the expected answer: false (line 17). They are not the same object in memory. Testing them using the equals() method in line 18 also returns the expected answer of true, which shows they have the same value.

Determining the Class of an Object

Want to find out the name of an object’s class? Here’s how for an object assigned to the variable key:

String name = key.getClass().getName();

The getClass() method is defined in the Object class, so it can be called in all Java objects. The method returns a Class object that represents the object’s class. That object’s getName() method returns a string holding the name of the class.

Another useful test is the instanceof operator, which has two operands: a reference to an object on the left, and a class name on the right. The expression produces a Boolean value: true if the object is an instance of the named class or any of that class’s subclasses, or false otherwise, as in these examples:

boolean check1 = "Texas" instanceof String; // true

Object obiwan = new Object();
boolean check2 = obiwan instanceof String; // false

The instanceof operator also can be used for interfaces. If an object implements an interface, the instanceof operator returns true when this is tested.

Unlike other operators in Java, instanceof is not a form of punctuation like * for multiplication or + for addition. Instead, the instanceof keyword is the operator.

  • + Share This
  • 🔖 Save To Your Account