Boolean
Types
Literals
ballerina/lang.boolean
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.
bal run boolean_literal.bal
Boolean Literal - Truth: true, False: false
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
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)
bal run boolean_fromString.bal
Boolean fromString - True: true, False: false
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
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
|
|
bal run boolean_lang_support.bal
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.
|
|
bal run boolean_lib.bal
Boolean Lang Library Functions:
fromString("TRUE"): true
fromString("FALSE"): false
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
Boolean Lang Library
ToString Behavior
|
|
bal run boolean_toString.bal
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();
}
Boolean toString Behavior
Conclusion
In this post, I have discussed the boolean
data type in Ballerina. Here are the key points:
- Boolean Literal, e.g.
true
&false
. - Lang Library : ballerina/lang.boolean.
- Used in many language constructs.