Beginner 10 min read

Boolean

By the end of this lesson, you'll be able to:

  • Explain what a Boolean represents
  • Declare and use a Boolean variable
  • Use a Boolean to represent a real business condition

Prerequisites: "Text: String"

Only two possible values

Boolean isActive = true;
Boolean hasDiscount = false;
  1. Boolean isActive = true; — declares a Boolean (true/false) variable, set to true.
  2. Boolean hasDiscount = false; — same type, set to false — there's no third option.

That's the entire type — no in-between, no "maybe." A Boolean is often the result of asking a question: "is this customer active?", "does this order qualify for a discount?"

A real business example: Healthcare

Boolean isInsured = true;
Boolean requiresFollowUp = false;
  1. Boolean isInsured = true; — records a yes/no fact about the patient.
  2. Boolean requiresFollowUp = false; — another yes/no fact — both are exactly the kind of question an if statement (next module) will act on.

A patient record system constantly deals in yes/no facts like these — and Module "Making Decisions" (right after this one) is entirely about acting on Booleans like these with if statements.

Common mistakes

  • Writing if (isActive == true) instead of if (isActive). Both work, but the second is the idiomatic, cleaner Apex style — a Boolean variable already is the true/false answer, no comparison needed.
  • Naming Booleans ambiguously. flag or status tells you nothing; isActive or hasDiscount tells you exactly what true means.

Exercise

Declare a Boolean called isEligibleForDiscount and set it to true. Debug it.

Show hint

This is as simple as it looks: Boolean isEligibleForDiscount = true;

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Boolean Quiz

1. How many possible values can a Boolean hold?

2. A well-named Boolean variable often starts with "is" or "has".

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A Boolean holds exactly one of two values: true or false. Despite being the simplest type in Apex, Booleans are what every business decision in your code ultimately boils down to.