OneBite.Dev - Coding blog in a bite size

count array's length in java

Code snippet on how to count array's length in java

int length = 0;
if (array != null) {
    length = array.length;
}

This piece of code checks if an array is not null and if it is not, it retrieves the length of the array. First, we declare an integer called length and set it to 0. Then we use an if statement to check if the array is not equal to null. If it is not, then we assign the length variable to the array’s length. If it is null, the length variable will be set to 0 and no further action will be taken.

java