No array size() method is available. However, the array contains a length field that can be used to specify the length or size of the array.
array.length: length is a finite variable that applies to arrays. With the help of variable length we can determine the size of the array.
Examples:
int size = arr[].length; // length can be used // for int[], double[], String[] // to know the length of the arrays.
Below is an illustration of how to get length of array[] in Java with variable length:
Example 1:
// Java program to illustrate // how to get the length of the array public class Test { public static void main(String[] args) { // Here array is the // array name of int type int[] array = new int[4]; System.out.println("The size of " + "the array is " + array.length); } }
Output:
The size of the array is 4
Example 2:
// Java program to illustrate // how to get the length of the array public class Test { public static void main(String[] args) { // Here str is the array name // of String type. String[] str = { "GEEKS", "FOR", "GEEKS" }; System.out.println("The size of " + "the array is " + str.length); } }
Output:
The size of the array is 3