import ballerina/io;
import ballerina/lang.'float as floats;
import ballerina/lang.'int as ints;
public function main() {
    int i = 10;
    io:println(i);
    int|error i2 = ints:fromString("100");
    if (i2 is int) {
        io:println(i2);
    }
    float f = 20.0;
    io:println(f);
    float f1 = floats:fromBitsInt(i);
    float f2 = 22.0;
    float max = floats:max(f1, f2);
    io:println("Max float: ", max);
    float nanVal = 0.0/0.0;
    io:println(nanVal.isNaN());
    float infiniteVal = 12.0/0.0;
    io:println(infiniteVal.isInfinite());
    float finiteVal = 6.0/3.0;
    io:println(finiteVal.isFinite());
    decimal d = 27.5;
    io:println(d);
    byte c = 23;
    io:println(c);
    string s = "Ballerina";
    io:println(s);
    boolean b = true;
    io:println(b);
    () n = ();
    io:println(n);
    json j = null;
    io:println(j);
}

Values

Ballerina programs operate on a universe of values and each value belongs to only one basic type such as int, boolean, map, record, function etc. There are three kinds of values corresponding to three kinds of basic types. They are simple values (e.g., int, string, boolean), structured values (e.g., record, map, array), and behavioral values (e.g., function, object). Simple values belong to one of the simple basic types, namely nil, boolean, int, float, decimal, and string.

import ballerina/io;
import ballerina/lang.'float as floats;
import ballerina/lang.'int as ints;
public function main() {

The types int, float, decimal, string, boolean, byte, and nil are called simple basic types because they are basic types with only simple values. Simple values are always immutable.

    int i = 10;
    io:println(i);

The int type represents the set of 64-bit signed integers.

    int|error i2 = ints:fromString("100");
    if (i2 is int) {
        io:println(i2);
    }

The ballerina/lang.int module contains common functions that can be used with int values.

    float f = 20.0;
    io:println(f);

The float type represents the set of double precision IEEE 754 floating point numbers.

    float f1 = floats:fromBitsInt(i);
    float f2 = 22.0;
    float max = floats:max(f1, f2);
    io:println("Max float: ", max);

The ballerina/lang.float module contains common functions that can be used with float values.

    float nanVal = 0.0/0.0;
    io:println(nanVal.isNaN());

The .isNaN(), .isInfinite(), and .isFinite() langlib functions are supported by the float type. The .isNaN() function will return true if the float value is neither finite nor infinite.

    float infiniteVal = 12.0/0.0;
    io:println(infiniteVal.isInfinite());

The .isInfinite() function will return true if the float value is neither NaN nor finite.

    float finiteVal = 6.0/3.0;
    io:println(finiteVal.isFinite());

The .isFinite() function will return true if the float value is neither NaN nor infinite.

    decimal d = 27.5;
    io:println(d);

The decimal type represents the set of 128-bits IEEE 754R decimal floating point numbers.

    byte c = 23;
    io:println(c);

The byte type represents the set of 8-bit unsigned integers.

    string s = "Ballerina";
    io:println(s);

The string type represents the set of sequences of Unicode code points.

    boolean b = true;
    io:println(b);

The boolean type has only two values: true and false.

    () n = ();
    io:println(n);

The nil type has a single value and is used to represent the absence of any other value. Both the nil type and the nil value are written as ().

    json j = null;
    io:println(j);
}

Another representation for the nil value is the null literal. However, the use of the null literal in only allowed in JSON contexts.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run values.bal
10
100
20.0
Max float: 22.0
true
true
true
27.5
23
Ballerina
true