Boolean Data type Usage and Type Conversion in Kotlin
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
Kotlin Data Types
- Int Data type Usage and Type Conversion in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Number Data type Usage and Type Conversion in Kotlin
- String Data type Usage and Type Conversion in Kotlin
- Elvis Operator in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin