Swan Lake Beta3 is here.
class MyClass {
    int i = 0;
}

public function main() {
    MyClass obj1 = new MyClass();
    MyClass obj2 = new MyClass();
    // `b1` will be true.
    boolean b1 = (obj1 === obj1);
    // `b2` will be false.
    boolean b2 = (obj1 === obj2);
    // `b3` will be true.
    boolean b3 = ([1,2,3] == [1,2,3]);
    // `b4` will be false.
    boolean b4 = ([1,2,3] === [1,2,3]);
    // `b5` will be true.
    boolean b5 = (-0.0 == +0.0);
    // `b6` will be false.
    boolean b6 = (-0.0 === +0.0);

}

Identity

=== and !== operators test for identity. Identical for mutable basic types means stored at the same address. == and != are not defined for objects. -0.0 and +0.0 are equal but not identical.

class MyClass {
    int i = 0;
}
public function main() {
    MyClass obj1 = new MyClass();
    MyClass obj2 = new MyClass();
    boolean b1 = (obj1 === obj1);

b1 will be true.

    boolean b2 = (obj1 === obj2);

b2 will be false.

    boolean b3 = ([1,2,3] == [1,2,3]);

b3 will be true.

    boolean b4 = ([1,2,3] === [1,2,3]);

b4 will be false.

    boolean b5 = (-0.0 == +0.0);

b5 will be true.

    boolean b6 = (-0.0 === +0.0);

b6 will be false.

}
bal run identity.bal
class MyClass {
    int i = 0;
}

public function main() {
    MyClass obj1 = new MyClass();
    MyClass obj2 = new MyClass();
    // `b1` will be true.
    boolean b1 = (obj1 === obj1);
    // `b2` will be false.
    boolean b2 = (obj1 === obj2);
    // `b3` will be true.
    boolean b3 = ([1,2,3] == [1,2,3]);
    // `b4` will be false.
    boolean b4 = ([1,2,3] === [1,2,3]);
    // `b5` will be true.
    boolean b5 = (-0.0 == +0.0);
    // `b6` will be false.
    boolean b6 = (-0.0 === +0.0);

}

Identity

=== and !== operators test for identity. Identical for mutable basic types means stored at the same address. == and != are not defined for objects. -0.0 and +0.0 are equal but not identical.

class MyClass {
    int i = 0;
}
public function main() {
    MyClass obj1 = new MyClass();
    MyClass obj2 = new MyClass();
    boolean b1 = (obj1 === obj1);

b1 will be true.

    boolean b2 = (obj1 === obj2);

b2 will be false.

    boolean b3 = ([1,2,3] == [1,2,3]);

b3 will be true.

    boolean b4 = ([1,2,3] === [1,2,3]);

b4 will be false.

    boolean b5 = (-0.0 == +0.0);

b5 will be true.

    boolean b6 = (-0.0 === +0.0);

b6 will be false.

}
bal run identity.bal
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand