Boolean
01. What is a Boolean?
A Boolean represents a binary logical state containing one of two possible values:true or false. It is named after George Boole, who defined an algebraic system of logic in the mid-19th century.
Booleans are the structural foundation of control flow and routing in software. Every branching decision (e.g., if statements, loops, evaluations) relies on computing a boolean value.
02. Key Characteristics
- Two Logical States: Booleans hold either
trueorfalse. In some languages, they may be capitalized (e.g.True/Falsein Python). - Logical Operators: Combined using logic gates: AND (intersection), OR (union), and NOT (negation).
- Size Allocation: Although a boolean logically requires only 1 bit of memory, computers address memory at the byte level. Therefore, booleans typically occupy a full 8-bit byte.
03. How Languages Represent It
At the processor level, there is no "boolean" register. CPUs evaluate logical conditions by comparing numbers:
- 0 (Zero): Represents False.
- Non-Zero: Represents True. Typically,
1is the standard compiler representation for true.
In strictly typed languages like Java, this mapping is hidden to prevent errors (you cannot add a boolean to an integer). In dynamic languages like Python or low-level languages like C/C++, booleans are directly interchangeable with integers.
04. Language Comparison
How different languages approach boolean primitives and logical truth:
- Python: Uses the
boolclass, which is a direct subclass ofint.Trueequals1, andFalseequals0. - Java: Features a dedicated
booleanprimitive type. It does not map to integers, and there is no concept of truthy/falsy evaluation for non-booleans. - JavaScript: Dynamic values are coerced to booleans when checked in conditions. Falsy values include
0,""(empty string),null,undefined, andNaN. - C: Traditionally lacked a boolean type. C99 introduced
boolvia the<stdbool.h>library, which maps to the integer-like type_Bool. - C++: Provides a native, built-in
booltype.
05. Code Examples
Select a tab below to see code declarations and usage:
# In Python, Boolean values are 'True' and 'False' (note the capitalization)
is_active = True
has_permission = False
print(type(is_active)) # Output: <class 'bool'>
# Boolean operators are word-based: and, or, not
can_enter = is_active and not has_permission
print(can_enter) # Output: True
# Booleans are integers under the hood (True = 1, False = 0)
print(True + True) # Output: 206. Key Differences
Key comparative differences regarding boolean typing and evaluation logic:
| Language | Boolean Class | Truthy/Falsy Coercion | Integer Cast Equivalent |
|---|---|---|---|
| Python | bool (int subclass) | Yes (empty lists, empty strings, 0, None are falsy) | Yes (True = 1, False = 0) |
| Java | boolean (primitive) | No (conditions MUST evaluate to boolean) | No (illegal compile cast) |
| JavaScript | boolean (primitive) | Yes (0, "", null, undefined, NaN are falsy) | Yes (coerces to 1 or 0) |
| C / C++ | bool | Yes (zero value is falsy, non-zero is truthy) | Yes (true = 1, false = 0) |
07. Common Mistakes
1. Using Assignment (=) instead of Equality (==)
In languages like C, C++, and JavaScript, writing if (isActive = false) accidentally assigns the valuefalse to the variable and evaluates to false. If you write if (x = 5), it assigns 5 and evaluates as true. Always use double == or triple === for comparison, or evaluate the variable directly: if (isActive).
2. Over-evaluating Boolean Expressions
Avoid writing redundant comparisons such as if (isValid == true). This makes code harder to read and introduces bugs. Write if (isValid) or if (!isValid) instead.
3. Confusing JavaScript Logical Coercion with Identity
In JavaScript, because of coercion rules, values that are truthy are not necessarily equivalent to the booleantrue under strict equality. For example, "hello" == true evaluates to false, even though if ("hello") will execute because it is truthy. Always use strict equality (===) to check booleans.