Kotlin Strings & Booleans
Estimated reading: 3 minutes 440 views

Kotlin – Booleans: Logical Operations & Comparisons

Introduction – Why Learn Kotlin Booleans?

Booleans are fundamental in decision-making. They represent true or false and are used to control program logic through conditions, loops, and comparisons. Kotlin provides robust Boolean operations, including logical operators, comparison operators, and short-circuiting behavior.

In this guide, you’ll learn:

  • How to declare and use Boolean variables
  • Kotlin logical operators (&&, ||, !)
  • Comparison operations that return Booleans
  • Best practices for clean conditional logic

Boolean Basics in Kotlin

Declaration and Initialization:

val isActive: Boolean = true
val isLoggedIn = false
  • Boolean is the Kotlin type.
  • Values can be either true or false.

Kotlin Logical Operators

Logical operators operate on Boolean expressions and return a Boolean result.

OperatorDescriptionExampleResult
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

Example:

val isMember = true
val hasPaid = false

if (isMember && hasPaid) {
    println("Access granted")
} else {
    println("Access denied")
}

Output:

Access denied

Kotlin Comparison Operators

These return Boolean values when comparing data types like Int, Double, String, etc.

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to4 != 3true
<Less than3 < 5true
>Greater than7 > 8false
<=Less than or equal to5 <= 5true
>=Greater than or equal10 >= 9true

Conditional Logic with Booleans

val age = 18
val isEligible = age >= 18

if (isEligible) {
    println("You can vote.")
} else {
    println("You are too young to vote.")
}

Output:

You can vote.

Smart Usage – Boolean in Expressions

You can use Booleans directly in conditions:

val isKotlinAwesome = true

if (isKotlinAwesome) {
    println("Yes, it is!")
}

Output:

Yes, it is!

Short-Circuit Evaluation

Kotlin uses short-circuit evaluation in && and ||.

val isValid = false
val result = isValid && expensiveCheck() // `expensiveCheck()` won't run

Efficient: Kotlin skips unnecessary evaluations to save performance.


Boolean Functions in Kotlin

You can write Boolean-returning functions:

fun isEven(n: Int): Boolean {
    return n % 2 == 0
}

println(isEven(4)) // true

Summary – Recap & Next Steps

Kotlin Booleans are simple but powerful. They enable clean, safe logic operations and comparisons across your codebase. With Kotlin’s strong typing, you’ll catch errors early and ensure correctness.

Key Takeaways:

  • Booleans store true or false values.
  • Logical operators: &&, ||, !
  • Comparison expressions return Booleans.
  • Use short-circuiting for efficiency in conditions.

Practical Use:
Booleans control program flow in Android apps, backend services, form validation, access control, and more.


FAQs – Kotlin Booleans

What values can a Kotlin Boolean hold?
Only true or false.


What is short-circuiting in Kotlin?
In && or ||, Kotlin stops evaluating as soon as the result is known:

false && expensiveCall() // expensiveCall() is skipped

Can Kotlin Booleans be nullable?
Yes. Use Boolean? to allow null:

val isChecked: Boolean? = null

Is true == 1 valid in Kotlin?
No. Kotlin is strictly typed. You must use conversion or logical checks:

val flag = (value == 1)

How to convert Boolean to String in Kotlin?
Use .toString():

val flag = true
println(flag.toString()) // "true"

Share Now :
Share

Kotlin – Booleans (Logical operations, comparisons)

Or Copy Link

CONTENTS
Scroll to Top