"jjj"
Kotlin Booleans
Kotlin Booleans – The True/False Switch
In programming, sometimes you just need a yes-or-no answer. That’s where Booleans come in handy.
Think of them as tiny flags that only flip between two states:
- true
- false
What is a Boolean?
A Boolean is a data type that only accepts two outcomes: true or false.
val isSunny: Boolean = true val isNight: Boolean = false println(isSunny) // true println(isNight) // false
Kotlin Figures It Out (Type Inference)
If you skip the Boolean label, Kotlin still knows what you mean:
val likesMusic = true val hatesNoise = false println(likesMusic) // true Println(hatesNoise) // false
No type declaration needed — Kotlin is clever that way!
Boolean Expressions – Logic in Action
You can use comparisons to generate Boolean values:
val a = 20 val b = 15 Println(a > b) // true, because 20 is more than 15
Or even shorter:
println(5 < 2) // false, because 5 is not less than 2
Equality Check
Want to see if values match? Use the double equal sign ==:
val score = 100 println(score == 100) // true Println(score == 90) // false
What’s the Point?
Boolean results are the foundation of decision-making in code.
They power up:
- if statements
- loops
- logical conditions
- and more...
Boolean Bits
| Concept | Example Code | Outcome |
|---|---|---|
| Declare Boolean | val happy: Boolean = true | true |
| Skip type | val cold = false | Kotlin infers |
| Compare values | println(10 > 3) | true |
| Check equality | println(4 == 5) | false |
| Store expression | val result = 7 <= 7 | true |
Prefer Learning by Watching?
Watch these YouTube tutorials to understand KOTLIN Tutorial visually:
What You'll Learn:
- 📌 Kotlin - Boolean Data Type
- 📌 Kotlin tutorial : Boolean