How to determine length or size of an Array in Java?
There is no size() method available with the array. But there is a length field available in the array that can be used to find the length or size of the array.
array.length: length is a final variable applicable for arrays. With the help of the length variable, we can obtain the size of the array.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
int size = arr.length; // length can be used // for int, double, String // to know the length of the arrays.
Below is the illustration of how to get the length of array in Java using length variable:
The size of the array is 4
The size of the array is 3