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);


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));


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));


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);


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
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step

This is a list of recommended tutorials or courses that might be useful before starting this one.

Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations