Kotlin - Booleans

Many times we come across a situation where we need to take decision in Yes or No, or may be we can say True or False. To handle such situation Kotlin has a Boolean data type, which can take the values either true or false.
Kotlin also has a nullable counterpart Boolean? that can have the null value.
A boolean variable can be created using Boolean keyword and this variable can only take the values either true or false:
fun main(args: Array<String>) {
   val isSummer: Boolean = true
   val isCold: Boolean = false
  
   println(isSummer)
   println(isCold)
   
}

When you run the above Kotlin program, it will generate the following output:
true
false

In fact, we can create Kotlin boolean variables without using Boolean keyword and Kotlin will understand the variable type based on  the assigned values either true or false
Kotlin provides following built-in operators for boolean variables. These operators are also called Logical Operators:
Following example shows different calculations using Kotlin Logical Operators:
fun main(args: Array<String>) {
   var x: Boolean = true
   var y:Boolean = false

   println("x && y = " +  (x && y))
   println("x || y = " +  (x || y))
   println("!y = " +  (!y))
}

When you run the above Kotlin program, it will generate the following output:
x && y = false
x || y = true
!y = true

A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.
fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x > y = " +  (x > y))
   println("x < y = " +  (x < y))
   println("x >= y = " +  (x >= y))
   println("x <= y = " +  (x <= y))
   println("x == y = " +  (x == y))
   println("x != y = " +  (x != y))
}

When you run the above Kotlin program, it will generate the following output:
x > y = true
x < y = false
x >= y = true
x <= y = false
x == y = false
x != y = true

Kotlin provides and() and or() functions to perform logical AND and logical OR operations between two boolean operands.

These functions are different from && and || operators because these functions do not perform short-circuit evaluation but they always evaluate both the operands.
fun main(args: Array<String>) {
   val x: Boolean = true
   val y: Boolean = false
   val z: Boolean = true

   println("x.and(y) = " +  x.and(y))
   println("x.or(y) = " +  x.or(y))
   println("x.and(z) = " +  x.and(z))
}

When you run the above Kotlin program, it will generate the following output:
x.and(y) = false
x.or(y) = true
x.and(z) = true

Kotlin also provides not() and xor() functions to perform Logical NOT and XOR operations respectively.
You can use toString() function to convert a Boolean object into its equivalent string representation.

You will need this conversion when assigning a true or false value in a String variable.
fun main(args: Array<String>) {
   val x: Boolean = true
   var z: String
   
   z = x.toString()

   println("x.toString() = " +  x.toString())
   println("z = " +  z)
}

When you run the above Kotlin program, it will generate the following output:
x.toString() = true
z = true

Q 1 - Which of the following is true about Kotlin Boolean Data type?
A - Boolean data type can have two values true and false
B - Boolean data type can have two values 0 and 1
C - We can assign Boolean value to integer variable
D - All of the above
Kotlin Boolean variable can have only two values either true or false
Q 2 - What will be the output of the following program:
fun main(args: Array<String>) {
   val x: Boolean = true
   var y: String
   
   y = x
}

A - This will compile successfully without error and warning
B - This will raise just a warning
C - Compilation will stop with error
D - None of the above
Compilation will stop with type mismatch error because we are trying to store a boolean value into a String variable. We should use toString() to convert Boolean value into string value before we assign it into string variable.

    68 Lectures 
     4.5 hours 

 Arnab Chakraborty

    71 Lectures 
     5.5 hours 

 Frahaan Hussain

    18 Lectures 
     1.5 hours 

 Mahmoud Ramadan

    49 Lectures 
     6 hours 

 Catalin Stefan

    49 Lectures 
     2.5 hours 

 Skillbakerystudios

    22 Lectures 
     1 hours 

 CLEMENT OCHIENG
 Print
 Add Notes
 Bookmark this page