Chapter 2 - Java for Beginners Course

Equality and Relational Operators

In different occasions, you’ll need to compare two values or variables, usually to make decisions that will affect the execution of the application.

Relational and equality operators serve this purpose, and are mainly used in conjunction with Control Flow statements that will be covered later. All these operators will return a boolean value to signal whether the comparison is true or false.

Equality Operators

The == (equal to) and != (not equal to) operators are used to compare 2 values to check if they are equal or different respectively.

For example:

int a = 1;
int b = 10;

boolean areDifferent = (a != b);
System.out.println("a and b are different? " + areDifferent);

boolean areEqual = (a == b);
System.out.println("a and b are equal? " + areEqual);

Output:

a and b are different? true
a and b are equal? false

When using the equality operator (==), please note the use of the two equals signs. Remember that a single equals sign is used for assignment purposes.

Effect when using object references

In Chapter 1, we had already seen the use of the equality operator but applied to objects instead of primitives to clarify the concept of references vs objects, for example:

Airplane plane1 = new Airplane();
Airplane plane2;
plane2 = plane1;
System.out.println("plane1 is pointing to the same instance as plane2: " + (plane1 == plane2));

Output:

plane1 is pointing to the same instance as plane2: true

The == and != operators can also be used with object references, as shown in the example above (plane1 == plane2), however they have a behaviour that might not be what you would normally expect.

When used with objects references, the == operator will return true only when the 2 references being compared point to the same object, or when both are equal to null.

Common pitfall with the == sign

Take the following snippet of code as an example:

// used to illustrate the behaviour of the == operator in object references
Integer x = new Integer(1);
Integer y = new Integer(1);
System.out.println("Value of x: " + x);
System.out.println("Value of y: " + y);
System.out.println("x and y point to the same object? " + (x == y));

Output:

Value of x: 1
Value of y: 1
x and y point to the same object? false

Here, we have 2 Integer objects, x and y, both with a value of 1. However, in memory they are 2 different objects. Note that we are using the new operator for both x and y which means they will be separate objects.

In this case, even though both Integers are equal (both have a value of 1), the object references x and y are not as they point to different objects.

To compare the objects and not the references, you can make use of the equals method when appropriate, or of the compareTo method when available. These will be covered in chapter 6. For example, x.equals(y)

Relational Operators

As well as checking if two values/variables are equal or not, you might need to check the relation between them. The following 4 operators, that follow the same convention as in mathematics, allow you to do exactly that:

Operator Name Result

<

Less than

True if the left operand is less than the right operand, false otherwise

<=

Less than or equal to

True if the left operand is less than or equal to the right operand, false otherwise

>=

Greater than or equal to

True if the left operand is greater than or equal to the right operand, false otherwise

>

Greater than

True if the left operand is greater than the right operand, false otherwise

For example:

int a = 1;
int b = 10;
int c = 1;

boolean lessThan = (a < b);
System.out.println("a is less than b? " + lessThan);

boolean lessOrEqual = (a <= c);
System.out.println("a is less than or equal to c? " + lessOrEqual);

boolean greaterThan = (a > b);
System.out.println("a is greater than b? " + greaterThan);

boolean greaterOrEqual = (a >= b);
System.out.println("a is greater than or equal to b? " + greaterOrEqual);

Output:

a is less than b? true
a is less than or equal to c? true
a is greater than b? false
a is greater than or equal to b? false