In Java, the array length is the number of elements that an array can holds. There is no predefined method to obtain the length of an array. We can find the array length in Java by using the array attribute length. We use this attribute with the array name. In this section, we will learn how to find the length or size of an array in Java.
Array length Attribute
Java provides an attribute length that determines the length of an array. Every array has an in-built length property whose value is the size of the array. Size implies the total number of elements that an array can contain. The length property can be invoked by using the dot [.] operator followed by the array name. We can find the length of int[], double[], String[], etc. For example:
In the above code snippet, arr is an array of type int that can hold 5 elements. The arrayLength is a variable that stores the length of an array. To find the length of the array, we have used array name [arr] followed by the dot operator and length attribute, respectively. It determines the size of the array.
Note that length determines the maximum number of elements that the array can contain or the capacity of the array. It does not count the elements that are inserted into the array. That is, length returns the total size of the array. For arrays whose elements are initialized at the time of its creation, length and size are the same.
If we talk about the logical size, the index of the array, then simply int arrayLength=arr.length-1, because the array index starts from 0. So, the logical or array index will always be less than the actual size by 1.
The length
data property of an Array
instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
Try it
Value
A non-negative integer less than 232.
Property attributes ofArray.prototype.length
WritableyesEnumerablenoConfigurablenoDescription
The value of the length
property is a non-negative integer with a value less than 232.
const listA = [1, 2, 3];
const listB = new Array[6];
console.log[listA.length];
// 3
console.log[listB.length];
// 6
listB.length = 2 ** 32; // 4294967296
// RangeError: Invalid array length
const listC = new Array[-100]; // Negative numbers are not allowed
// RangeError: Invalid array length
The array object observes the length
property, and automatically syncs the length
value with the array's content. This means:
- Setting
length
to a value smaller than the current length truncates the array — elements beyond the newlength
are deleted. - Setting any array index [a non-negative integer smaller than 232] beyond the current
length
extends the array — thelength
property is increased to reflect the new highest index. - Setting
length
to an invalid value [e.g. a negative number or a non-integer] throws a
7 exception.const arr = [1, 2]; console.log[arr]; // [ 1, 2 ] arr.length = 5; // set array length to 5 while currently 2. console.log[arr]; // [ 1, 2, ] arr.forEach[[element] => console.log[element]]; // 1 // 2
When length
is set to a bigger value than the current length, the array is extended by adding empty slots, not actual
const arr = [1, 2];
console.log[arr];
// [ 1, 2 ]
arr.length = 5; // set array length to 5 while currently 2.
console.log[arr];
// [ 1, 2, ]
arr.forEach[[element] => console.log[element]];
// 1
// 2
9 values. Empty slots have some special interactions with array methods; see array methods and empty slots.const arr = [1, 2];
console.log[arr];
// [ 1, 2 ]
arr.length = 5; // set array length to 5 while currently 2.
console.log[arr];
// [ 1, 2, ]
arr.forEach[[element] => console.log[element]];
// 1
// 2
See also Relationship between length
and numerical properties.
Examples
Iterating over an array
In the following example, the array
const numbers = [1, 2, 3, 4, 5];
const length = numbers.length;
for [let i = 0; i