diff --git a/examples/binary-data/binary_data.md b/examples/binary-data/binary_data.md index f493c0231c..cdcaaf9036 100644 --- a/examples/binary-data/binary_data.md +++ b/examples/binary-data/binary_data.md @@ -8,6 +8,5 @@ Binary data is represented by arrays of byte values. It is a special syntax for ## Related links - [Arrays](/learn/by-example/arrays) +- [Byte type](/learn/by-example/byte-type) - [Integers](/learn/by-example/integers) - -[comment]: # (Add byte type BBE link) diff --git a/examples/byte-type/byte_type.bal b/examples/byte-type/byte_type.bal new file mode 100644 index 0000000000..1fa78778cc --- /dev/null +++ b/examples/byte-type/byte_type.bal @@ -0,0 +1,12 @@ +import ballerina/io; + +public function main() { + // The `byte` type consists of integers ranging from `0` to `255`. + byte b = 255; + io:println(b); + + // Since the set of possible `byte` values is a subset of `int` values, + // the `byte` type is a subtype of the `int` type. + int i = b; + io:println(i); +} diff --git a/examples/byte-type/byte_type.md b/examples/byte-type/byte_type.md new file mode 100644 index 0000000000..f732a45ebf --- /dev/null +++ b/examples/byte-type/byte_type.md @@ -0,0 +1,7 @@ +# Byte type + +The byte type in Ballerina represents an 8-bit unsigned integer, with values ranging from 0 to 255. + +::: code byte_type.bal ::: + +::: out byte_type.out ::: \ No newline at end of file diff --git a/examples/byte-type/byte_type.metatags b/examples/byte-type/byte_type.metatags new file mode 100644 index 0000000000..aa883b2787 --- /dev/null +++ b/examples/byte-type/byte_type.metatags @@ -0,0 +1,2 @@ +description: This BBE introduces the Ballerina byte type. +keywords: ballerina, ballerina by example, bbe, bytes, byte, byte type diff --git a/examples/byte-type/byte_type.out b/examples/byte-type/byte_type.out new file mode 100644 index 0000000000..8aea7bfd1a --- /dev/null +++ b/examples/byte-type/byte_type.out @@ -0,0 +1,3 @@ +$ bal run byte_type.bal +255 +255 diff --git a/examples/expression-equality/expression_equality.bal b/examples/expression-equality/expression_equality.bal new file mode 100644 index 0000000000..b124e1bf87 --- /dev/null +++ b/examples/expression-equality/expression_equality.bal @@ -0,0 +1,51 @@ +import ballerina/io; + +public function main() { + map student = {"name": "John", "age": "25"}; + map student2 = {"name": "John", "age": "25"}; + + // The `==` check evaluates to `true` since the values are considered equal based on the + // equality of members. + io:println(student == student2); + + // The `!=` check evaluates to `false` since the values are considered equal based on the + // equality of members. + io:println(student != student2); + + // The `===` check evaluates to `false` since references are different. + io:println(student === student2); + + // Assign the value assigned to the `student` variable to the `student3` variable. + map student3 = student; + + // The `===` check evaluates to `true` since references are same. + io:println(student3 === student); + + // The `!==` check evaluates to `false` since references are same. + io:println(student3 !== student); + + // Since values of simple types do not have storage identity + // `===` and `==` return the same result, except for floating point values. + int a = 1; + anydata b = 1; + + // The `==` and `===` checks evaluates to `true` since the values are equal. + io:println(a == b); + io:println(a === b); + + decimal c = 1.0; + decimal d = 1.00; + + // The `==` check evaluates to `true` since the values are equal. + io:println(c == d); + // The `===` check evaluates to `false` since `c` and `d` are distinct values with different precision. + io:println(c === d); + + string s1 = "Hello"; + string s2 = "Hello"; + + // String values also do not have storage identity, and `===` checks are + // the same as `==` checks for string values also. + io:println(s1 == s2); + io:println(s1 === s2); +} diff --git a/examples/expression-equality/expression_equality.md b/examples/expression-equality/expression_equality.md new file mode 100644 index 0000000000..5741bd1b7b --- /dev/null +++ b/examples/expression-equality/expression_equality.md @@ -0,0 +1,12 @@ +# Expression equality + +Expression equality is determined using two operators: `==` for value equality and `===` for reference equality. The `==` operator checks for deep equality between two values by comparing the actual data. In contrast, the `===` operator checks whether two values share the same storage identity, meaning it checks if the values reference the same memory location. + +Values with storage identity, such as structured types like maps and arrays, have an identity that comes from the location where the value is stored. For such values, the `===` check determines if two references point to the same location. For simple types such as integers and booleans, which do not have storage identity, `===` behaves the same as `==` except for floating point values. + +::: code expression_equality.bal ::: + +::: out expression_equality.out ::: + +## Related links +- [Maps](/learn/by-example/maps) diff --git a/examples/expression-equality/expression_equality.metatags b/examples/expression-equality/expression_equality.metatags new file mode 100644 index 0000000000..40789bf305 --- /dev/null +++ b/examples/expression-equality/expression_equality.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates expression equality checks in Ballerina. +keywords: ballerina, ballerina by example, bbe, ==, !=, ===, !==, equality diff --git a/examples/expression-equality/expression_equality.out b/examples/expression-equality/expression_equality.out new file mode 100644 index 0000000000..52f6647e17 --- /dev/null +++ b/examples/expression-equality/expression_equality.out @@ -0,0 +1,12 @@ +$ bal run expression_equality.bal +true +false +false +true +false +true +true +true +false +true +true diff --git a/examples/index.json b/examples/index.json index 386ec561ef..670866fb12 100644 --- a/examples/index.json +++ b/examples/index.json @@ -103,6 +103,13 @@ "verifyBuild": true, "verifyOutput": true, "isLearnByExample": true + }, + { + "name": "Byte type", + "url": "byte-type", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true } ] }, @@ -415,6 +422,20 @@ } ] }, + { + "title": "Equality", + "column": 0, + "category": "Language concepts", + "samples": [ + { + "name": "Expression equality", + "url": "expression-equality", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + } + ] + }, { "title": "Lists", "column": 0, diff --git a/examples/list-equality/list_equality.md b/examples/list-equality/list_equality.md index d35398aecb..a0dec651c2 100644 --- a/examples/list-equality/list_equality.md +++ b/examples/list-equality/list_equality.md @@ -9,6 +9,5 @@ You can use `==` and `!=` on lists to check the deep equality of two lists: two ## Related links - [Tuples](/learn/by-example/tuples) - [Arrays](/learn/by-example/arrays) +- [Expression equality](/learn/by-example/expression-equality) - [List sub typing](/learn/by-example/list-subtyping) - -[comment]: # (Add equality expression link) diff --git a/examples/security-crypto/security_crypto.md b/examples/security-crypto/security_crypto.md index 5e11301426..6d4d27c202 100644 --- a/examples/security-crypto/security_crypto.md +++ b/examples/security-crypto/security_crypto.md @@ -13,4 +13,4 @@ Run the program by executing the command below. ::: out security_crypto.out ::: ## Related links -- [Binary data](/learn/by-example/binary-data/) \ No newline at end of file +- [Binary data](/learn/by-example/binary-data/)