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

The boolean Type: true and false

Learn that boolean variables hold only two values: true or false, with no numeric equivalents.

The boolean Type: true and false

What you'll learn: You'll discover Java's simplest data type that represents yes/no, on/off logic with just two possible values.

What is a boolean?

The boolean type is Java's dedicated tool for storing truth values. Unlike some programming languages where numbers like 0 and 1 represent false and true, Java keeps things crystal clear: a boolean variable can only hold one of two literal values: true or false.

Think of it like a light switch—it's either ON or OFF, with no in-between state and no numeric meaning attached.

Declaring and Using boolean Variables

You declare boolean variables just like other primitive types:

boolean isJavaFun = true;
boolean isRaining = false;

Notice that true and false are keywords in Java—they're built into the language itself. You don't write them in quotes (that would make them text, not boolean values).

No Numeric Tricks

This is crucial: Java's boolean type has no numeric equivalent. You cannot do this:

boolean flag = 1;  // COMPILER ERROR!
boolean answer = 0;  // COMPILER ERROR!

Java enforces strict type safety here. A boolean is purely logical, not mathematical. This prevents common bugs found in languages where 0 means false and any other number means true.

Why This Matters

Boolean values are the foundation of decision-making in programs. They represent answers to yes/no questions: Is the user logged in? Has the file been saved? Did the operation succeed? Java's strict separation between boolean logic and numbers makes your code's intent clearer and safer.

Key Takeaway: The boolean type holds exactly two values—true or false—with no numeric equivalents or conversions allowed, making it Java's purest form of logical data.