Updated April 6, 2023
Introduction to Kotlin Boolean
In kotlin boolean is one of the data types that can be supported only for true or false values it supports all other operators like a relational operator that can be resulted only with the boolean value it prints and validates the values to the console screen it also compares the different objects with under specified conditions the index value is varied if the condition is satisfied then it will execute the loop based on the dependency calculates the programming code the object comparison will be performed to the specified order if the object is equal it returns the true statement else it will return a negative value.
Syntax
In kotlin programming, language datatype is the most important one for declaring and utilizing the values. Like a boolean is one of the data types and it also considers as a primitive datatype it satisfies only true or false statement.
fun main()
{
var variable : Boolean ?= null;
if (variable == true){
----some logics---
}
if(variable == false)
{
---some logic codes---
}
}
The above code is the basic syntax for validating the boolean conditions and it satisfies all the programming logics and loop dependencies which overwhelmed by the coding functionalities.
How does boolean work in Kotlin?
Basically, kotlin is strong, and also it is the statically typed programming language for each and every variable that deals with the expression and it’s the type that can be used for compiled programming language. So that kotlin type is the limit type of values that can be used to hold the variable values and its expressions for producing the limit operations supported on these values and the operations. Sometimes the static type helps to detect errors at the compile-time it does not support the feature for implicit conversion between the datatypes. Most often we do not use the explicit datatype for the variables that can be interfered with the kotlin type inference for determining the datatype. The boolean class will wrap the value of the primitive type boolean objects it contains the single field datatype additionally this class provides many default methods for converting the boolean to the other datatypes like string etc. We can also possibly for converting the other types like String to Boolean which will as some constants and built-in methods for using to validate the boolean statements. The boolean object allocated the default value argument as passing to the method whenever we call it in other areas.
Examples of Kotlin Boolean
Below are the different examples:
Example #1
Code:
open class first {
open fun demo1() {
println("Welcome To My Domain its the first example that related to the kotlin boolean datatype")
}
}
abstract class second : first() {
override abstract fun demo1()
}
class Third: second(){
override fun demo1() {
println("We can validate the two operands value by using the boolean type")
}
}
fun main() {
try{
val vars: Boolean
vars = false
println("Thank you users the specified operand value is validated either true or false"+"$vars")
println("If the operand is the boolean attribute then the try statement will executed continuously otherwise it exist the loop")
throw Exception("It throws and catches the exception and error which is thrown by variable")
println("Your exceptions are printed")
}
catch(e: Exception){
val new = first()
new.demo1()
val th = Third()
th.demo1()
println(e)
}
finally{
println("Thank you users have a nice day for spending your valuable time with our kotlin application")
}
var hs = HashSet<Int>(8)
hs.add(123)
hs.add(456)
hs.add(789)
hs.add(175)
hs.add(197)
hs.add(824)
hs.add(456)
hs.add(2344)
println("Thank you users for choosing the collection concepts in the boolean application")
for (s in hs){
println(s)
}
var new1: HashSet<Int> = hashSetOf<Int>(1,4,3,2,7,8,9,122)
val new2 = setOf(3,6,9,12)
println("THe hashset values stored and iterated using the for or other loops")
for (x in new1){
println(x)
}
println("The first set of hashset size is calculated")
println(new1.size)
println("We used to calculate the hashset method like contains() method")
println(new1.contains(13))
println("The other default method like containsAll() built-in method")
println(new1.containsAll(new2))
}
Output:
In the above example, we used boolean along with hashset collections and their default methods.
Example #2
Code:
fun main() {
if (true is Boolean){
print("Thank you users the mentioned variable true is a boolean value")
}
var varfloat = 411F
println("Welcome to My Domain here float datatype value is used ${varfloat}")
var p: Float = Float.MIN_VALUE
var p2: Float = Float.MAX_VALUE
println("Your Float datatype value is calculates if the value is minimum: " +p)
println("Else the mentioned value is maximum: " + p2)
var dou1: Double = Double.MIN_VALUE
var dou2: Double = Double.MAX_VALUE
println("The mentioned double type value is minimum" + dou1)
println("The mentioned double type value is maximum " + dou2)
var inp = 456
var lng = 41L
println("Your integer datatype value is ${inp}")
println("Your long datatype value is ${lng}")
var by1: Byte = Byte.MIN_VALUE
var by2: Byte = Byte.MAX_VALUE
println("Your byte value is minimum: " +by1)
println("Your byte datatype value is maximum " +by2)
var sh1: Short = Short.MIN_VALUE
var sh2: Short = Short.MAX_VALUE
println("Your short datatype value is minimum " +sh1)
println("Your short datatype value is maximum " +sh2)
var in1: Int = Int.MIN_VALUE
var in2: Int = Int.MAX_VALUE
println("Your first input integer value is minimum " +in1)
println("Your second input integer value is maximum " +in2)
var log1: Long = Long.MIN_VALUE
var log2: Long = Long.MAX_VALUE
println("Your first long type value is minimum" +log1)
println("Your first long type value is maximum " +log2)
var ch: Char = 'S'
println("Your input s is the character : $ch is Char type")
}
Output:
In the second example we used a boolean and another datatype with some basic declarations.
Example #3
Code:
fun main() {
var inp1 ="July"
var inp2 = "June"
println(inp2)
val month: Boolean
month=true
if ( month==false) {
println("Thank you users this month is $inp1")
}
if (month==true) {
println("Thank you users sorry this month is $inp2")
}
}
Output:
In the final example, we used two variables with string type and to validate the values using if condition along with the boolean statement.
Conclusion
In the kotlin language, the boolean data type is the most predominant and it can be validated along with the other operands with different data types like integer, string, etc. It supports only true and false statements if the input condition is satisfied it will execute the loop conditions else it will exit the loop.
Recommended Articles
This is a guide to Kotlin Boolean. Here we discuss the introduction, syntax, and working of boolean in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –