Java booleans are one of the pro­gram­ming 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 math­em­atician George Boole, are elements from algebra that describe a variable that can only have a certain number of values. In pro­gram­ming, the principle is used to create logic and link the execution of an ap­plic­a­tion to a condition. If the condition is fulfilled (that is, if it’s true), the ap­plic­a­tion will be executed. If the condition is not fulfilled, the ap­plic­a­tion is not executed.

When working with pro­gram­ming languages, there are a lot of situ­ations with only two con­ceiv­able states. Some examples are the options on and off, the answers yes and no and the values true and false. When learning a pro­gram­ming 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.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

How to create a Java boolean

Creating a Java boolean is simple. The syntax looks as follows:

boolean booleanexample1 = true;
boolean booleanexample2 = false;
java

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:

public class Main {
	public static void main(String[] args) {
		boolean x = true;
		boolean y = false;
		System.out.println(x);
		System.out.println(y);
	}
}
java

The output looks as follows:

true
false
java

How to use Java booleans in if-else state­ments

In practice, Java booleans are used in com­bin­a­tion with other state­ments to stipulate that the result of an eval­u­ation needs to be a Boolean value. Let’s look at how this works with if-else state­ments.

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

In this case, the output will be:

The condition is: true
java

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 ter­min­ates.

In our example, we’ll ini­tial­ise 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++;
		}
	}
}
java

The output will look as follows:

10
11
12
13
14
15
16
17
18
19
20
java

When the variable x reaches the value 21, the loop is ter­min­ated.

How to use a Boolean ex­pres­sion

In many cases, rather than creating a boolean value, you can just use a Boolean ex­pres­sion. Boolean ex­pres­sions 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.");
		}
	}
}
java

The output looks as follows:

School is in session.
java

741035376cac1f90cb41b8537f90d01f

994c68ed11aa4313a4ce3dec2c3c7dc9

8397fd36c49b00fac5c9a87e7e1b7c18

bbbdfdef6e0703a48e076573665ea466

7e42dcec1f35084dfd873196edad2dca

Go to Main Menu