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;
Boolean isActive = true;— declares aBoolean(true/false) variable, set totrue.Boolean hasDiscount = false;— same type, set tofalse— 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;
Boolean isInsured = true;— records a yes/no fact about the patient.Boolean requiresFollowUp = false;— another yes/no fact — both are exactly the kind of question anifstatement (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 ofif (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.
flagorstatustells you nothing;isActiveorhasDiscounttells you exactly whattruemeans.
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;
Boolean Quiz
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.