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:
boolean booleanexample1 = true;
boolean booleanexample2 = false;
javaIn 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:
public class Main {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
System.out.println(x);
System.out.println(y);
}
}
javaThe output looks as follows:
true
false
javaHow 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.
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 10;
boolean x1 = true;
boolean y1 = false;
if (y > x) {
System.out.println("The condition is: " + x1);
}
else {
System.out.println("The condition is: " +y1);
}
}
}
javaIn this case, the output will be:
The condition is: true
javaHow 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:
public class Main {
public static void main(String[] args) {
int x = 10;
while (x <= 20) {
System.out.println(x);
x++;
}
}
}
javaThe output will look as follows:
10
11
12
13
14
15
16
17
18
19
20
javaWhen 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:
public class Main {
public static void main(String[] args) {
int currentsnow = 3;
int snowlimit = 5;
if (currentsnow <= snowlimit) {
System.out.println("School is in session.");
} else {
System.out.println("School is canceled.");
}
}
}
javaThe output looks as follows:
School is in session.
java