Skip to content

Array comparision #37567

Aug 30, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

With int[] arrayGm = arrayNm; arrayGm refers to the same array value referred to by arrayNm. Therefore, any change done to the array value via arrayGm happens on the same array value that is assigned to arrayNm. So both variables refer to the same array value, which is why even after the change the equality check will result in true anyway.

If what's required is to create a copy of the array value, the lang.value:clone langlib function (int[] arrayGm = arrayNm.clone();) can be used.

We can use the exact equality (===) check to check if the variables refer to the same value.

import ballerina/io;

public function main() {
    int[] a = [2, 3, 4, 5, 4];
    int[] b = a;

    io:println(a == b)…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@navaneeth-algorithm
Comment options

Answer selected by navaneeth-algorithm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants