Home »
Scala
Scala Boolean Data Type
By IncludeHelp Last updated : October 07, 2024
Boolean
A Boolean is a data type in Scala programming language (and another programming also), that is used or Boolean algebra. It has two valid values i.e. true and false.
- We declare it using the var keyword, with optional data type specified as Boolean.
- The default value for this data type is false.
- Also, for the compiler, the true value is treated as 1 and false is treated as 0.
- The size of the Boolean datatype is 1 bit.
- Boolean values are used to check for checking conditions of conditional statements, loops, etc.
Syntax to declare a Boolean variable
//Syntax with data type
var bool : Boolean = true;
//Syntax without data type
var a = false;
When we don't specify the data type explicitly the compiler assigns the variable data type based on the value.
Scala Example of Boolean Data Type
object MyClass {
def add(x:Int, y:Int) = x + y;
def main(args: Array[String]) {
var bool = true;
var a : Boolean = false
if(bool){
println("the value of the bool variable is true");
}
if(a){
println("the value of the a variable is true")
}
else
println("the value of the a variable is false");
bool = false //this is also possible
}
}
Output
the value of the bool variable is true
the value of the a variable is false
Code Logic
The code is to show the use of a Boolean variable. There are two boolean variables defined a and bool. bool is created without specifying the data type and a is created with data type and two if statements check for their value that leads to the generation of output.