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­e­mati­cian 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­pli­ca­tion to a condition. If the condition is fulfilled (that is, if it’s true), the ap­pli­ca­tion will be executed. If the condition is not fulfilled, the ap­pli­ca­tion is not executed.

When working with pro­gram­ming languages, there are a lot of sit­u­a­tions 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
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

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­bi­na­tion with other state­ments to stipulate that the result of an eval­u­a­tion 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­mi­nates.

In our example, we’ll ini­tial­ize 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­mi­nat­ed.

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
Go to Main Menu