Dremendo Tag Line

Boolean Variable in Java Programming

Java Basic Concepts

In this lesson, we will understand what is Boolean Variable and how to declare it in Java programming, with the help of some examples. We will also play a quiz at the end.

What is Boolean Variable

In Java program a boolean variable can store value either true or false. To declare a variable of type Boolean we use the keyword boolean.


Syntax of Declaring Boolean Variable in Java

boolean variable_name;

Here boolean is used for declaring Boolean data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).

Now let's see some examples for more understanding.

Example 1

Declare a boolean variable x.

boolean x;

Example 2

Declare 3 boolean variables x, y, and z to assign the values true, false and true respectively.

boolean x=true, y=false, z=true;

Example 3

Declare a boolean variable x and assign the value false and change it value to true in the next line.

boolean x = false;
x = true; //now the new value of x is true

Test Your Knowledge

Attempt the multiple choice quiz to check if the lesson is adequately clear to you.

Test Your Knowledge