Article
Object Oriented Concepts in Java - Part 1
Comparing Objects
At the end of the previous article in this series, I promised that this article would help to demystify how Strings work in Java. Specifically, I promised to explain how two Strings could be compared for equality in a Java program. Well, to those of you who have been waiting two weeks by your email Inbox for the answer, I would like to say, "Get a life."
Seriously though, the method of comparing for equality is another important difference between basic data values and Objects in Java. Consider the following code fragment:
1 int a = 5;
2 int b = 5;
3 if (a == b) System.out.println("a and b are equal!");
4
5 Tree t1 = new Tree();
6 t1.height = 5;
7 Tree t2 = new Tree();
8 t2.height = 5;
9 if (t1 == t2) System.out.println("t1 and t2 are equal!");
Once again, this code exposes different behaviour for basic data values and Objects. While the comparison on line 3 will find that a and b are indeed equal, the fact that the heights of the two Trees t1 and t2 are equal on line 9 is not enough for t1 and t2 themselves to be found equal. In fact, for two variables containing Objects to be found equal using the == operator as above, the two variables must both refer to the exact same object! The following code, for example, will find t1 and t2 to be equal:
1 Tree t1 = new Tree();
2 t1.height = 5;
3 Tree t2 = t1; // Both t1 and t2 refer to the same Tree
4 if (t1 == t2) System.out.println("t1 and t2 are equal!");
This form of comparison for equality is not especially useful though, is it?
Since Java uses a built-in class called String to represent text strings, the same problem occurs when you try to compare two strings for equality. For example, you might want to check if a value that was entered by the user is the correct password to obtain access to some feature of the program. Here's what you might be tempted to do:
1 if (enteredPassword == "secret") {
2 // Allow access
3 } else {
4 System.out.println("Access denied.");
5 }
The problem, however, is that even if the variable enteredPassword did contain the String "secret", it would not be the same instance of the String class as the value it was being compared to, so the comparison would always evaluate to false, and access would always be denied.
The solution is to use the equals method provided by the String class instead of the == operator. This method lets you compare one string with another to check if they contain the same text. Here's how to use the equals method in the above example:
1 if ( enteredPassword.equals("secret") ) {
2 // Allow access
3 } else {
4 System.out.println("Access denied.");
5 }
The equals method is different from other methods we've seen so far in that it takes a parameter. Instead of following the name of the method (equals) with an empty pair of parentheses, the parameter (in the case of equals, the String to compare to for equality) is typed between the parentheses. The equals method also results in a Boolean value (either true or false) depending on whether the Strings are equal or not. Don't worry too much about the details of these special features; we'll be seeing more examples of methods that take parameters and return values in part two of this article.
All of Java's built-in classes support the equals method to check for equality, and the documentation for each of these classes explains under which conditions two Objects of the class are considered equal. To enable comparisons for equality for Objects of your own classes, you should implement your own equals methods in those classes. I'll show you how to write an equals method for the Tree and CoconutTree classes in part two of this article.
To see a list of all the methods supported by String Objects, check out the Java API documentation for this class. Not all of it will make complete sense to you yet at this stage, but a lot of it will. For instance, look up the equalsIgnoreCase method, which compares two Strings for equality while ignoring case.