Java .length
In Java, the .length
property is used to determine the length or size of an array. It is a built-in property for arrays and returns an integer value that represents the number of elements in the array. Understanding how to use .length
is crucial for iterating through arrays, avoiding out-of-bounds errors, and dynamically managing data.
Syntax
arrayName.length;
Parameters:
arrayName
: The name of the array to call.length
on.
Return Value:
The .length
property returns an integer value that represents the number of elements in the array.
Example 1: Basic Usage of .length
This example uses the .length
property to determine the array length:
public class Array {public static void main(String[] args) {// Create an arrayString[] colors = {"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"};// Find the size of the arrayint length = colors.length;// Print the resultSystem.out.println("The length of the array is: " + length);}}
Since the colors
array contains 7 elements, the output for the example will be:
The length of the array is: 7
Example 2: Using .length
for Looping Through an Array
This example uses the .length
property for looping through an array and printing the elements in it:
public class Loop {public static void main(String[] args) {// Create an arrayString[] fruits = {"Apple", "Banana", "Cherry"};// Loop through the array and print the elements in itfor (int i = 0; i < fruits.length; i++) {System.out.println(fruits[i]);}}}
The output for the example will be:
AppleBananaCherry
Example 3: Using .length
with Multi-Dimensional Arrays
This example uses the .length
property to determine the number of rows and number of columns in the first row of a multi-dimensional array:
public class MultiDimensionalArray {public static void main(String[] args) {// Create a multi-dimensional arrayint[][] numbers = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};// Print the number of rows in the arraySystem.out.println("The number of rows in the array: " + numbers.length);// Print the number of columns in the first row of the arraySystem.out.println("The number of columns in the first row of the array: " + numbers[0].length);}}
The output for the example will be:
The number of rows in the array: 3The number of columns in the first row of the array: 3
Frequently Asked Questions
1. What is the difference between .length
and .length()
?
.length
is an array property, whereas .length()
is a string method.
2. Can the length of an array be changed?
No. Once an array is created, its length is constant and cannot be changed. To work with dynamic sizes, consider using ArrayList
.
3. What happens if I access an index beyond the array’s length?
You’ll get an ArrayIndexOutOfBoundsException
error, which is a runtime error.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours