#Booleans
Last modified: 05 May 2026
The Boolean type represents logical values: true and false.
Use Boolean values in functions that answer yes-or-no questions, and in the while, if, and when conditions.
#Declare a Boolean variable
To declare a Boolean variable, assign it true or false. You can specify the Boolean type explicitly or let Kotlin infer it from the value:
kotlinval isTrue: Boolean = true
val isFalse = false // Kotlin infers Boolean
If a value can be null, use Boolean?:
kotlinval isEnabled: Boolean? = null
Note: You cannot assign an integer value to a Boolean variable. In Kotlin,0and1are not Boolean values.
#Produce Boolean values
You can use comparison expressions and functions to produce Boolean values:
kotlinval number = 10
val isPositive = number > 0
println(isPositive) // true
val language = "Kotlin"
val isEmpty = language.isEmpty()
println(isEmpty) // false
You can use the results in conditions and other expressions as well:
kotlinval number = 10
val isPositive = number > 0 // true
if (isPositive) {
println("The number is positive.")
}
#Boolean operations
Kotlin provides operators and infix functions for working with Boolean values. You can use them to invert a Boolean value or combine multiple Boolean values into a single result.
#Negation (NOT)
The NOT operator inverts a Boolean value. To use NOT, place the ! operator before a Boolean value:
kotlinval isOn = true
val isOff = !isOn // isOff is false
#Logical AND
The AND operator returns true only if both operands are true. To use logical AND, place the && operator between operands:
kotlinval a = false && false // false
val b = false && true // false
val c = true && false // false
val d = true && true // true
If the first operand is false, the && operator skips the second operand. To evaluate both operands, use the and infix function instead.
#Logical OR
The OR operator returns true if at least one operand is true. To use logical OR, place the || operator between operands:
kotlinval a = false || false // false
val b = false || true // true
val c = true || false // true
val d = true || true // true
If the first operand is true, the || operator skips the second operand. To evaluate both operands, use the or infix function instead.
#Exclusive OR (XOR)
The exclusive OR (XOR) operation returns true if the operands have different values. To use XOR, write xor between operands:
kotlinval a = false xor false // false
val b = false xor true // true
val c = true xor false // true
val d = true xor true // false
xor is an infix function, not an operator.
#Operator precedence
If an expression contains multiple logical operations and no parentheses to specify the evaluation order, Kotlin applies precedence rules.
| Precedence | Operator/Function |
|---|---|
| 1 | ! |
| 2 | xor (and other infix functions) |
| 3 | && |
| 4 | ` |
In the following example, the compiler evaluates && before ||:
kotlinval result = true || false && false
println(result) // true
To make evaluation order explicit, use parentheses:
kotlinval result = (true || false) && false
println(result) // false
#Boolean in conditions
if, when, and while evaluate Boolean expressions to direct program flow.
#if expressions
kotlinval number = 4
val isEven = number % 2 == 0
// Condition already has the Boolean type
// You do not need to compare it to true or false
if (isEven) {
println("The number is even.")
} else {
println("The number is odd.")
}
#when expressions
kotlinval number = 3
when {
number > 0 -> println("The number is positive.")
number < 0 -> println("The number is negative.")
else -> println("The number is zero.")
}
#while loops
kotlinvar isCalculating = true
while (isCalculating) {
println("Calculating...")
isCalculating = false
}