Boolean Data type Usage and Type Conversion in Kotlin
On this page (7sections)
Definition
- Boolean data type represents only two values.Either true or false.
- The data type can be either declared explicitly or the compiler itself has the ability to infer the type of the assigned value.
- In JVM, the characteristics of this “Long” variable is derived from the characteristics of primitive type “long” of Java which has a non-nullable default value.
Syntax and declaration
Thus a Boolean value can be declared by 3 ways as mentioned below.
1. Mentioning type explicitly
val variable_name : Boolean = true
for example,
val check : Boolean = false
2. Data type is automatically inferred
val variable_name = true
for example,
val check = false
3. Declaring and Initializing separately
var variable_name : Boolean
variable_name = true
for example
//Declaring and Initializing separately
var check:Boolean
check = true
Boolean Datatype Example Program in Kotlin
// Boolean Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs
fun main(args:Array<String>) {
//Assigning value to Boolean variable explicitly
val a:Boolean = true
//Declaring a Boolean value immediately
val b:Boolean = false
println("Boolean value in a is $a")
println("Boolean value in b is $b")
}
Sample Output
Boolean value in a is true
Boolean value in b is false