By Barry Burd

When you start working with objects in Java, you find that you can use == and != to compare objects with one another. For instance, a button that you see on the computer screen is an object. You can ask whether the thing that was just mouse-clicked is a particular button on your screen. You do this with Java’s equality operator.

if (e.getSource() == bCopy) {
    clipboard.setText(which.getText());

The big gotcha with Java’s comparison scheme comes when you compare two strings. When you compare two strings with one another, you don’t want to use the double equal sign. Using the double equal sign would ask, “Is this string stored in exactly the same place in memory as that other string?” Usually, that’s not what you want to ask.

Instead, you usually want to ask, “Does this string have the same characters in it as that other string?” To ask the second question (the more appropriate question) Java’s String type has a method named equals. (Like everything else in the known universe, this equals method is defined in the Java API, short for Application Programming Interface.)

The equals method compares two strings to see whether they have the same characters in them. For an example using Java’s equals method, see this code listing. (The figure shows a run of the program in the listing.)

import static java.lang.System.*;
import java.util.Scanner;
public class CheckPassword {
    public static void main(String args[]) {
        out.print("What's the password? ");
        Scanner keyboard = new Scanner(in);
        String password = keyboard.next();
        out.println("You typed >>" + password + "<<");
        out.println();
        if (password == "swordfish") {
            out.println("The word you typed is stored");
            out.println("in the same place as the real");
            out.println("password. You must be a");
            out.println("hacker.");
        } else {
            out.println("The word you typed is not");
            out.println("stored in the same place as");
            out.println("the real password, but that's");
            out.println("no big deal.");
        }
        out.println();
        if (password.equals("swordfish")) {
            out.println("The word you typed has the");
            out.println("same characters as the real");
            out.println("password. You can use our");
            out.println("precious system.");
        } else {
            out.println("The word you typed doesn't");
            out.println("have the same characters as");
            out.println("the real password. You can't");
            out.println("use our precious system.");
        }
        keyboard.close();
    }
}

In the listing, the call keyboard.next() grabs whatever word the user types on the computer keyboard. The code shoves this word into the variable named password. Then the program’s if statements use two different techniques to compare password with “swordfish”.

The results of using == and using Java’s equals method.

The results of using == and using Java’s equals method.

The examples in the printed book are mostly text-based, but you can find fancier versions of most examples on Dummies website. These fancier versions have windows, buttons, text fields, and other elements of a typical graphical user interface (GUI).

The more appropriate of the two techniques uses Java’s equals method. The equals method looks funny because when you call it, you put a dot after one string and put the other string in parentheses. But that’s the way you have to do it.

In calling Java’s equals method, it doesn’t matter which string gets the dot and which gets the parentheses. For instance, in the listing, you could have written

if ("swordfish".equals(password)) 

The method would work just as well.

A call to Java’s equals method looks imbalanced, but it’s not. There’s a reason behind the apparent imbalance between the dot and the parentheses. The idea is that you have two objects: the password object and the “swordfish” object.

Each of these two objects is of type String. (However, password is a variable of type String, and “swordfish” is a String literal.) When you write password.equals(“swordfish”), you’re calling an equals method that belongs to the password object. When you call that method, you’re feeding “swordfish” to the method as the method’s parameter (pun intended).

When comparing strings with one another, use the equals method — not the double equal sign.