KotlinBooleans
Kotlin Booleans
Very often, in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, Kotlin has aBoolean data type, which can take the valuestrue orfalse.
Boolean Values
A boolean type can be declared with theBoolean keyword and can only take the valuestrue orfalse:
Example
val isKotlinFun: Boolean = trueval isFishTasty: Boolean = falseprintln(isKotlinFun) // Outputs trueprintln(isFishTasty) // Outputs falseTry it Yourself »Just like you have learned with other data types in the previous chapters, the example above can also be written without specifying the type, as Kotlin is smart enough to understand that the variables are Booleans:
Example
val isKotlinFun = trueval isFishTasty = falseprintln(isKotlinFun) // Outputs trueprintln(isFishTasty) // Outputs falseTry it Yourself »Boolean Expression
A Boolean expressionreturnsa Boolean value:true orfalse.
You can use a comparison operator, such as thegreater than (>) operator to find out if an expression (or a variable) is true:
Example
val x = 10val y = 9
println(x > y) // Returns true, because 10 is greater than 9Try it Yourself »Or even easier:
In the examples below, we use theequal to (==) operator to evaluate an expression:
Example
val x = 10;println(x == 10); // Returns true, because the value of x is equal to 10Try it Yourself »The Boolean value of an expression is the basis for all Kotlin comparisons and conditions.
You will learn more about conditions in the next chapter.

