Lesson 4

Hi there! Today, we're entering the fascinating world of **Java comparison operators**. These operators play a vital role in controlling the flow of code by comparing values.

Our aim is to understand comparison operators and their use in Java programs. We'll explore various Java comparison operators and expand your knowledge through real-world examples.

Imagine piloting a starship in space. Here, destinations depend on evaluating conditions such as the distances to planets. These decisions boil down to comparisons, mirroring situations in programming. In Java, we utilize comparison operators to make such decisions.

Java has six comparison operators, including *equal to* (`==`

), *not equal to* (`!=`

), *greater than* (`>`

), *less than*(`<`

), *greater than or equal to* (`>=`

), and *less than or equal to* (`<=`

). They return `true`

or `false`

, also known as boolean values.

For instance, consider this comparison of a spaceship's speed with that of an asteroid:

Java`1int spaceshipSpeed = 20000; // speed in mph 2int asteroidSpeed = 15000; // speed in mph 3System.out.println("Is spaceship faster than an asteroid? " + (spaceshipSpeed > asteroidSpeed)); 4// Prints: Is a spaceship faster than an asteroid? true`

In the code above, we compared `spaceshipSpeed`

and `asteroidSpeed`

using the `>`

operator. The result is `true`

because `spaceshipSpeed`

is indeed higher than `asteroidSpeed`

.

Let's explore the *equal to* (`==`

) and *not equal to* (`!=`

) operators. These are essential when you need to compare values, such as comparing the current fuel level to the required one:

Java`1int currentFuelLevel = 90; // current fuel level in % 2int requiredFuelLevel = 100; // required fuel level in % 3 4boolean isEnoughFuel = currentFuelLevel == requiredFuelLevel; // this results in 'false' 5boolean isFuelLow = currentFuelLevel != requiredFuelLevel; // this results in 'true'`

The `==`

operator checks whether `currentFuelLevel`

equals `requiredFuelLevel`

, producing a `false`

result. Conversely, the `!=`

operator verifies their inequality, returning `true`

.

Next, let's examine the *less than* (`<`

), *greater than* (`>`

), *less than or equal to* (`<=`

), and *greater than or equal to* (`>=`

) operators. These are primarily used for the comparison of numeric data. Suppose you're observing two planets and want to determine which one is closer. You can use these operators to find out:

Java`1int distanceToPlanetA = 5000; // distance in kms 2int distanceToPlanetB = 3000; // distance in kms 3 4boolean isACloser = distanceToPlanetA < distanceToPlanetB; // this results in 'false' 5boolean isBCloserOrSame = distanceToPlanetA >= distanceToPlanetB; // this results in 'true'`

We compared the distances to two planets. Our spaceship isn't closer to *planet A*, thus `isACloser`

yields `false`

. However, the spaceship is closer or at an equal distance to *planet B*, so `isBCloserOrSame`

returns `true`

.

Great job! We've explored the comparison operators in Java and their practical applications. With this knowledge, you can manage decisions and effectively control the flow of your Java programs. Next, we'll delve deeper with hands-on practice exercises. Buckle up, and let's dive in!