Course contentsShow
← Java
Lesson 53 of 2,8702. Variables and Primitive TypesFree lesson

Declaring and Initializing boolean Variables

Practice declaring boolean variables and understand that boolean expressions evaluate to boolean values.

Declaring and Initializing boolean Variables

What you'll learn: How to create boolean variables and set them to true or false.

What is a boolean Variable?

A boolean variable is like a light switch—it's either on or off, with no in-between. In Java, a boolean can only hold one of two values: true or false. This makes it perfect for representing yes/no decisions, on/off states, or any binary condition.

Declaring a boolean Variable

Just like with int, char, or double, you declare a boolean variable by stating its type followed by a name:

boolean isRaining;
boolean hasLicense;
boolean isComplete;

At this point, these variables exist but have no value assigned yet.

Initializing a boolean Variable

To give a boolean variable an initial value, use the assignment operator with either true or false:

boolean isRaining = true;
boolean hasLicense = false;
boolean isComplete = false;

You can also declare first and initialize later:

boolean isOpen;
isOpen = true;

Why boolean Variables Matter

Boolean variables store the results of yes/no questions about your program's state. They're the foundation for making decisions in code. Notice that true and false are not strings—they're special keywords in Java that represent boolean values directly.

Key Takeaway: A boolean variable holds only true or false, declared with the syntax boolean variableName = true; or boolean variableName = false;, and represents binary states in your program.