HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Very often, in programming, you will need a data type that can only have one of two values, like
Very often, in programming, you will need a data type that can only have one of two values, like:
For this, Kotlin has a Boolean
data type, which can take the values true
or false
.
A boolean type can be declared with the Boolean
keyword and can only take the values true
or false
:
val isKotlinFun: Boolean = true
val isFishTasty: Boolean = false
println(isKotlinFun) // Outputs true
println(isFishTasty) // Outputs false
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:
val isKotlinFun = true
val isFishTasty = false
println(isKotlinFun) // Outputs true
println(isFishTasty) // Outputs false
A Boolean expression returns a Boolean value: true
or false
.
You can use a comparison operator, such as the greater than (>
) operator to find out if an expression (or a variable) is true:
val x = 10
val y = 9
println(x > y) // Returns true, because 10 is greater than 9
Or even easier:
println(10 > 9) // Returns true, because 10 is greater than 9
In the examples below, we use the equal to (==
) operator to evaluate an expression:
val x = 10;
println(x == 10); // Returns true, because the value of x is equal to 10
println(10 == 15); // Returns false, because 10 is not equal to 15
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.