- Creating New Objects
- Using Class and Instance Variables
- Calling Methods
- References to Objects
- Casting Objects and Primitive Types
- Comparing Object Values and Classes
- Summary
- Q&A
- Quiz
- Certification Practice
- Exercises
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 to see whether an object is an instance of a given class
Comparing Objects
Yesterday, you learned about operators for comparing values—equal to, 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, a method of the class called equals() 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 and save the file, either by choosing File, Save or by clicking the Save All toolbar button.
Listing 3.5. The Full Text of EqualsTester.java
1: class EqualsTester { 2: public static void main(String[] arguments) { 3: String str1, str2; 4: str1 = "Free the bound periodicals."; 5: str2 = str1; 6: 7: System.out.println("String1: " + str1); 8: System.out.println("String2: " + str2); 9: System.out.println("Same object? " + (str1 == str2)); 10: 11: str2 = new String(str1); 12: 13: System.out.println("String1: " + str1); 14: System.out.println("String2: " + str2); 15: System.out.println("Same object? " + (str1 == str2)); 16: System.out.println("Same value? " + str1.equals(str2)); 17: } 18: }
Here’s the output:
Output ▾
String1: Free the bound periodicals. String2: Free the bound periodicals. Same object? true String1: Free the bound periodicals. String2: Free the bound periodicals. Same object? false Same value? true
The first part of this program declares two variables (str1 and str2), assigns the literal “Free the bound periodicals.” to str1, and then assigns that value to str2 (lines 3–5). As you learned earlier, str1 and str2 now point to the same object, and the equality test at line 9 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 (line 15) returns the expected answer: false. They are not the same object in memory. Testing them using the equals() method in line 16 also returns the expected answer of true, which shows they have the same value.
Determining the Class of an Object
Want to find out what an object’s class is? Here’s how you do so 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 objects. It 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 Point pt = new Point(10, 10); boolean check2 = pt 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 defined as some form of punctuation. Instead, the instanceof keyword is the operator.