Boolean Data type Usage and Type Conversion in Kotlin
On this page (9sections)
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
How It Works
This Kotlin program demonstrates Boolean Data type Usage and Type Conversion. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Frequently Asked Questions
What is Boolean Data type Usage and Type Conversion in Kotlin?
Boolean data type represents only two values.Either true or false.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Boolean Data type Usage and Type Conversion?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.