Home » Scala language

Boolean data type in Scala

Boolean in Scala: A Boolean is a data type that consists of only two values. Scala also supports Boolean values. In this Scala tutorial, we will learn about Boolean data types with a working example.
Submitted by Shivang Yadav, on July 14, 2019

Scala type | 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 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.

Example for Boolean data type in Scala

object MyClass {
    def add(x:Int, y:Int) = x + y;
    def main(args: Array[String]) {
        var bool = true; 
        var a : Boolean = false
            println("the value of the bool variable is true");
            println("the value of the a variable is true")
            println("the value of the a variable is false");
        bool = false //this is also possible 


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.


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.