Please enable JavaScript to use CodeHS

Chapter 3

Boolean Expressions and if Statements

3.7 Comparing Objects

In this section, you are going to look at how to compare objects. As you may recall from previous sections, objects, or reference variables, differ in several ways from primitive variables. The important difference that you will explore in this lesson is the way that the values are stored. Recall that primitive variables store actual values for the variable while objects store a reference to the memory location of the value.

Comparing Objects with == and !=

The == comparison operator is designed to compare the value of two variables. Since the value that is stored for a reference variable is the memory location, using == will return whether or not the two objects have the same reference. When two objects have the same reference, they are said to be aliases.

If the objects are aliases of one another, then you know that they will also be equal.

Take a look at the example below:

Rectangle one = new Rectangle(3,7);
Rectangle two = one;

boolean same = one == two; // Will be true
Plain text

In this example, the two rectangles are the same and they are also aliases of one another. As a result, comparing the objects with == returns true.

You can also do this comparison with != to check if two objects are not aliases, and both can be used to compare to null to determine whether the reference is actually to an object.

Rectangle three;
boolean isSet = three != null; // Will be false
Plain text

In this example, you can see the isSet variable will be set to false since variable three has not yet been set to reference an object.

Comparing Using .equals()

Using == allowed us to find if two reference variables were aliases, but what if you want to find if two reference variables are the same?

For example:

Rectangle one = new Rectangle(3,7);
Rectangle two = new Rectangle(3,7);
Plain text

Are rectangles one and two the same? In this case, they appear to be, however, if you test them with ==, Java will return false. Why is that?

Recall above that you saw how == compares the memory locations of the variables. Since both rectangles were instantiated as separate objects, they are stored in a different place in memory, so using == will return false.

Objects can have several pieces of information associated with them. As a result, an object needs to have a specific method that can be used to determine if they are equal. For example, what would make two rectangles equal? Would it be that the rectangles have the same dimensions or can two rectangles be equal if they have different dimensions, but the same area?

In Java, the method that is used is called .equals(). This method is called from a variable and it passes a parameter to compare to in order to determine if the values are equal.

If you assume that one rectangle equals another rectangle if they have the same width and height, then .equals() can return true if the method compares the width and height of two rectangles and finds them to be the same.

Take a look at the example below:

Rectangle one = new Rectangle(3,7);
Rectangle two = new Rectangle(3,7);
boolean isAlias = one == two; // Will be set to false
boolean isEqual = one.equals(two); // Will be set to true
Plain text

In this example, you can see that one and two are not aliases of one another. If you compare them with the comparison operator, it will return false. Since both rectangles were created the same way, it seems logical that they are in fact equal and by using the .equals(), you can see that this is the case.

Comparing Strings

Comparing Rectangles

Null Test

Identify Aliases

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What is the proper way to compare String values in Java?

Exercise: Three Strings

Write a program that asks the user for three strings.

Then, print out whether or not the first string is equal to the second and third string concatenated together. Don’t worry about spaces at the beginning or end of strings unless you want to!

Here are a few sample program runs:

Sample Program 1:

First string? peppermint
Second string? pepper
Third string? mint
pepper + mint is equal to peppermint!
Plain text

Sample Program 2:

First string? donuts
Second string? go
Third string? fish
go + fish is not equal to donuts!
Plain text