How to use Java booleans
Java booleans are one of the programming language’s primitive data types. The boolean is a truth value that can only have one of two possible values, usually “true” or “false”. Booleans in Java are the basis for numerous commands and actions. In this tutorial, we’ll take a closer look at how they work with some examples.
What are Java booleans?
Boolean values, named after the English mathematician George Boole, are elements from algebra that describe a variable that can only have a certain number of values. In programming, the principle is used to create logic and link the execution of an application to a condition. If the condition is fulfilled (that is, if it’s true), the application will be executed. If the condition is not fulfilled, the application is not executed.
When working with programming languages, there are a lot of situations with only two conceivable states. Some examples are the options on and off, the answers yes and no and the values true and false. When learning a programming language, it quickly becomes clear how important Boolean values are. There are countless processes that are based on the idea that one of two states will hold. This is, for example, the case when deciding whether to execute a Java command based on a state. Booleans are the data type that are typically used for such tasks.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How to create a Java boolean
Creating a Java boolean is simple. The syntax looks as follows:
In the following basic example, we’ll see how a boolean is created and then returned. We’ll define two values, one true and one false:
The output looks as follows:
How to use Java booleans in if-else statements
In practice, Java booleans are used in combination with other statements to stipulate that the result of an evaluation needs to be a Boolean value. Let’s look at how this works with if-else statements.
In this case, the output will be:
How to use Java booleans in a while loop
Java booleans can also be combined with while loops. While loops are executed as long as the value is true. When that is no longer the case, the loop terminates.
In our example, we’ll initialize the value x with 10 and instruct the system to run the loop while the value is less than or equal to 20. In each iteration the value will be increased by 1, which we indicate with the increment operator ++
. Here’s how this looks in practice:
The output will look as follows:
When the variable x reaches the value 21, the loop is terminated.
How to use a Boolean expression
In many cases, rather than creating a boolean value, you can just use a Boolean expression. Boolean expressions follow the same logic but in a shorter and clearer form. We already saw one in our above example with the Java operator <=
(less than or equal to).
In our next example, let’s imagine that a school will declare a snow day if there is more than 5 inches of snow. If there is 5 inches of snow or less, school will take place as scheduled. The code for this looks as follows:
The output looks as follows: