.length
Published Mar 3, 2023
Contribute to Docs
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.
Syntax
The syntax of the .length
property is the following:
arrayName.length;
arrayName
: The name of the array to call.length
on.
Example
The example below uses the .length
property to determine the size of an array:
int[] numbers = {1, 2, 3, 4, 5};int length = numbers.length;System.out.println("Length of the array is" + length)
In this example, the numbers
array contains five elements, and the .length
property returns the value 5
, which is assigned to the length
variable.
Output for the above code will be:
Length of the array is 5
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 Friendly16 hours