# How to use Java booleans

Java booleans are one of the programming language’s [primitive data types](https://www.ionos.ca/digitalguide/websites/web-development/java-primitives/). 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](https://www.ionos.ca/digitalguide/websites/web-development/web-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](https://www.ionos.ca/digitalguide/websites/web-development/which-programming-language-to-learn/), 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](https://www.ionos.ca/digitalguide/websites/web-development/java-commands/) based on a state. Booleans are the data type that are typically used for such tasks.

## How to create a Java boolean

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

```java
boolean booleanexample1 = true;
boolean booleanexample2 = false;
```

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:

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

The output looks as follows:

```java
true
false
```

## 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.

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

In this case, the output will be:

```java
The condition is: true
```

## 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:

```java
public class Main {
	public static void main(String[] args) {
		int x = 10;
		while (x <= 20) {
			System.out.println(x);
			x++;
		}
	}
}
```

The output will look as follows:

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

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](https://www.ionos.ca/digitalguide/websites/web-development/java-operators/) `<=` (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:

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

The output looks as follows:

```java
School is in session.
```


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/javas-boolean-primitive-data-type/](https://www.ionos.ca/digitalguide/websites/web-development/javas-boolean-primitive-data-type/) for AI/LLM consumption.