Boolean data type

Boolean is a data type that contains one of two values—true or false. Because of its nature, the Boolean variable requires only one single bit for storing data. This bit can be on or off, true or false, 0 or 1. Let's declare some Boolean variables for further discussion:

var result : Boolean = true
var isEmpty : Boolean = false

In the same way as other variables, the Boolean variable can be declared without type inference:

var value = false
var result = true

Basically, this data type is used for comparing two values, getting results by setting a Boolean as a checkpoint to verify the results via comparison, and getting an answer by way of true or false. Let's see an example of how this can happen:

fun main(args: Array<String>) {

var result : Boolean // Boolean variable
var num1 = 20
var num2 = 10

result = num1 >= num2
println("$num1 is greater than $num2 = $result")

result = num1 < num2
println("$num1 is greater than $num2 = $result")
}

In this example, we have two integer variables named num1 and num2, as well as a Boolean variable named result. If num1 is greater than or equal to num2, true will be assigned to result; otherwise, false will be assigned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.254.44