Post

Boolean Data Type

The Boolean data type represents boolean truth values: true and false. You may use boolean values to represent various conditions in your program. For example, you can use a boolean value to represent whether a file exists or not, whether a user is logged in or not, etc. Use-cases of boolean values are endless.

✍ Syntax

boolean

✍ Syntax

Boolean type is written as boolean. We use the Boolean literal syntax to write Boolean values. A boolean true is written as true and a boolean false is written as false. In Ballerina,0, 1, or any other variations of true or false words are not supported as alternative syntax.

1
2
3
4
5
6
7
8
9
import ballerina/io;

public function main() {

    boolean truth = true;
    boolean falseValue = false;

    io:println(string `Boolean Literal - Truth: ${truth}, False: ${falseValue}`);
}
Boolean Literal - Truth: true, False: false
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    boolean truth = true;
    boolean falseValue = false;
}
// Highlights Only. Change View  to see full code.
boolean truth = true;
boolean falseValue = false;

Boolean Literal

Value Creation

In addition to the Boolean literal syntax, we can create a Boolean value using following methods.

  • boolean:fromString - Create a Boolean value from a String.
  • Boolean Expression - The result of a Boolean expression is a Boolean value. (See next section)
1
2
3
4
5
6
7
8
9
import ballerina/io;

public function main() returns error? {

    boolean fromStringTrue = check boolean:fromString("true");
    boolean fromStringFalse = check boolean:fromString("false");

    io:println(string `Boolean fromString - True: ${fromStringTrue}, False: ${fromStringFalse}`);
}
Boolean fromString - True: true, False: false
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() returns error? {
    boolean fromStringTrue = check boolean:fromString("true");
    boolean fromStringFalse = check boolean:fromString("false");
}
// Highlights Only. Change View  to see full code.
boolean fromStringTrue = check boolean:fromString("true");
boolean fromStringFalse = check boolean:fromString("false");

Creating a Boolean Value from a string

Properties and Operations

boolean values do not have a storage identity, so they are inherently immutable (once created, it is not possible to change the content of the value). Therefore, they can be assigned to a readonly variable.

The boolean type part of the anydata and json. Also, it is an ordered type.

In Ballerina, boolean values are used in many operations and statements. Here are some of those. I will discuss these operations in detail in later posts.

  • Unary logical expression - ! x
  • The result of a Relational expression - x < y, x > y, x <= y, x >= y
  • The result of an Equality expression - x == y, x != y, x === y, x !=== y
  • Logical expression - x | y, x && y, x || y
  • if statement
  • Conditional expression x ? y : z
  • match statement (in match guard expression)
  • while statement
  • For writing Transactional Retry Managers
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import ballerina/io;

function demoSupportedOperations() {

    boolean x = true;
    boolean y = false;

    string output = string `Logical Operators:
    x && y: ${x && y}
    x || y: ${x || y}
    !x: ${!x}
    `;
    io:println(output);

    int a = 5;
    int b = 10;

    output = string `Various Boolean Expressions:
    a == b: ${a == b}
    a != b: ${a != b}
    a < b: ${a < b}
    a <= b: ${a <= b}
    a > b: ${a > b}
    a >= b: ${a >= b}
    a === b: ${a === b}
    a !== b: ${a !== b}
    a is b: ${a is int}
    a !is b: ${b !is int}
    `;
    io:println(output);
}

function demoLanguageUsage(boolean isTrue = true) {

    if isTrue {
        io:println("If Statement: It's true");
    } else {
        io:println("If Statement: It's false");
    }

    string result = isTrue ? "It's true" : "It's false";
    io:println("Ternary Expression: ", result);

    int i = 0;
    while i < 5 { // This boolean expression is used to determine whether to continue looping or not.
        io:println("While Loop Iteration: ", i);
        i = i + 1;
    }
}

public function main() returns error? {
    demoSupportedOperations();
    demoLanguageUsage();
}
Logical Operators:
    x && y: false
    x || y: true
    !x: false
    
Various Boolean Expressions:
    a == b: false
    a != b: true
    a < b: true
    a <= b: true
    a > b: false
    a >= b: false
    a === b: false
    a !== b: true
    a is b: true
    a !is b: false
    
If Statement: It's true
Ternary Expression: It's true
While Loop Iteration: 0
While Loop Iteration: 1
While Loop Iteration: 2
While Loop Iteration: 3
While Loop Iteration: 4
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
function demoSupportedOperations() {
    boolean x = true;
    boolean y = false;
    string output = string `Logical Operators:
    x && y: ${x && y}
    x || y: ${x || y}
    !x: ${!x}
    `;

    int a = 5;
    int b = 10;
    output = string `Various Boolean Expressions:
    a == b: ${a == b}
    a != b: ${a != b}
    a < b: ${a < b}
    a <= b: ${a <= b}
    a > b: ${a > b}
    a >= b: ${a >= b}
    a === b: ${a === b}
    a !== b: ${a !== b}
    a is b: ${a is int}
    a !is b: ${b !is int}
    `;
}

function demoLanguageUsage(boolean isTrue = true) {
    if isTrue {
    } else {
    }

    string result = isTrue ? "It's true" : "It's false";

    int i = 0;
    while i < 5 { // This boolean expression is used to determine whether to continue looping or not.
        i = i + 1;
    }
}
// Highlights Only. Change View  to see full code.
    boolean x = true;
    boolean y = false;
    string output = string `Logical Operators:
    x && y: ${x && y}
    x || y: ${x || y}
    !x: ${!x}
    `;

    int a = 5;
    int b = 10;
    output = string `Various Boolean Expressions:
    a == b: ${a == b}
    a != b: ${a != b}
    a < b: ${a < b}
    a <= b: ${a <= b}
    a > b: ${a > b}
    a >= b: ${a >= b}
    a === b: ${a === b}
    a !== b: ${a !== b}
    a is b: ${a is int}
    a !is b: ${b !is int}
    `;

    if isTrue {
    } else {
    }

    string result = isTrue ? "It's true" : "It's false";

    while i < 5 { // This boolean expression is used to determine whether to continue looping or not.
    }

Boolean Usage Examples

Language Library - ballerina/lang.boolean

ballerina\lang.boolean Lang Library provides functions that operate on a boolean value. See available operations with examples in Boolean Lang Library.

Additionally, ballerina/lang.value Lang Library provides toString and another set of functions that can operate on a boolean value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import ballerina/io;

public function main() returns error? {

    boolean fromStringTrue = check boolean:fromString("TRUE");
    boolean fromStringFalse = check boolean:fromString("FALSE");

    string output = string `Boolean Lang Library Functions:
    fromString("TRUE"): ${fromStringTrue}
    fromString("FALSE"): ${fromStringFalse}
    `;
    io:println(output);
}
Boolean Lang Library Functions:
    fromString("TRUE"): true
    fromString("FALSE"): false
    
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() returns error? {
    boolean fromStringTrue = check boolean:fromString("TRUE");
    boolean fromStringFalse = check boolean:fromString("FALSE");
}
// Highlights Only. Change View  to see full code.
boolean fromStringTrue = check boolean:fromString("TRUE");
boolean fromStringFalse = check boolean:fromString("FALSE");

Boolean Lang Library

ToString Behavior

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import ballerina/io;

public function main() {

    boolean trueValue = true;
    boolean falseValue = false;
    string trueString = trueValue.toString();
    string falseString = falseValue.toString();
    string trueJsonString = trueValue.toJsonString();
    string falseJsonString = falseValue.toJsonString();

    string output = string `ToString Behavior:
    True to String: ${trueString}
    False to String: ${falseString}
    True to Json String: ${trueJsonString}
    False to Json String: ${falseJsonString}
    `;
    io:println(output);
}
ToString Behavior:
    True to String: true
    False to String: false
    True to Json String: true
    False to Json String: false
    
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    boolean trueValue = true;
    boolean falseValue = false;
    string trueString = trueValue.toString();
    string falseString = falseValue.toString();
    string trueJsonString = trueValue.toJsonString();
    string falseJsonString = falseValue.toJsonString();
}
// Highlights Only. Change View  to see full code.
boolean trueValue = true;
boolean falseValue = false;
string trueString = trueValue.toString();
string falseString = falseValue.toString();
string trueJsonString = trueValue.toJsonString();
string falseJsonString = falseValue.toJsonString();

Boolean toString Behavior

Conclusion

In this post, I have discussed the boolean data type in Ballerina. Here are the key points:


Boolean Data Type

Navigation

Site Settings

Site Theme

Source Code Density