For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
In Java, arrays are used to store multiple values of the same type in a single variable. Once you create an array, its size is fixed. This means you need a reliable way to find how many elements it holds, and that's where array length comes in.
Understanding how to check the size of an array is essential for looping, validations, and memory handling. If you’re just getting started with arrays or Java, this blog will walk you through everything you need to know about array length. It will also cover syntax, examples, best practices and real-world use cases of Java Array Length.
Learning Java through structured software engineering courses can help you grasp such topics faster and better.
In Java, every array has a built-in property called length. This property tells you how many elements the array can store. Unlike other languages like Python or JavaScript, where array length can change dynamically, Java arrays have a fixed length once declared.
You can use this property to access, loop through, or perform checks on the array.
Example:
int[] marks = {85, 90, 78, 92};
System.out.println("Length: " + marks.length);
Output:
Length: 4
Explanation: The array marks holds 4 elements, so marks.length returns 4. Note that length is a property, not a method.
Must explore: What is Memory Allocation in Java?
You can access the length of any array using the following syntax:
arrayName.length
Remember: there are no parentheses because length is a field, not a method.
Example:
String[] cities = {"Delhi", "Mumbai", "Chennai"};
System.out.println("Total Cities: " + cities.length);
Output:
Total Cities: 3
Explanation: We are accessing the length of the array cities, which contains 3 elements.
Enhance your abilities through these best-in-class courses/certifications.
Java offers a few ways to determine array size depending on what you're trying to do. While the .length property is the most direct way, you can also use other techniques in specific scenarios.
The .length property is the most direct and common way to get the size of an array. It returns the total number of elements stored. It’s simple and efficient since every array in Java internally stores its length as a final field.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length);
Output:
5
Time Complexity: O(1)
Loops use the .length property to iterate through each element. Although you're not calculating the length manually, for or for-each Loop method reinforces how .length works in practical use, like accessing or processing each array item without going out of bounds.
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i < vowels.length; i++) {
System.out.print(vowels[i] + " ");
}
Output:
a e i o u
Time Complexity: O(n)
Java 8 introduced streams, and you can convert an array into a stream using Arrays.stream(). Then use .count() to get the number of elements. This is useful when working with functional programming or needing to chain other operations.
int[] arr = {1, 2, 3};
long count = Arrays.stream(arr).count();
System.out.println(count);
Output:
3
Time Complexity: O(n)
You can convert an array to a list using Arrays.asList() and then call .size() to get the count. This is especially helpful when working with collections or needing additional list features, but it's slightly less efficient for simply getting array length.
String[] langs = {"Java", "Python", "C++"};
List<String> list = Arrays.asList(langs);
System.out.println(list.size());
Output:
3
Time Complexity:
Also read: Java ArrayList forEach
Different data types in Java use the same .length property. Here's how it works across common array types:
int[] nums = {1, 2, 3, 4};
System.out.println(nums.length); // Output: 4
String[] names = {"Aliya", "Bhanu", "Charu"};
System.out.println(names.length); // Output: 3
To get the row length and column length:
int[][] matrix = {
{1, 2},
{3, 4},
{5, 6}
};
System.out.println(matrix.length); // Rows: 3
System.out.println(matrix[0].length); // Columns: 2
Object[] items = {"Book", 100, true};
System.out.println(items.length); // Output: 3
Must explore: Control Statements in Java: What Do You Need to Know in 2025
Incorrect:
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // Error
1. Looping Through Arrays to Process Data
Array length helps loop through elements safely, preventing out-of-bound errors during iteration.
2. Validating Input Size Before Processing
Use array length to ensure the input has enough elements before performing operations.
3. Dynamically Copying or Truncating Arrays
Array length allows you to copy or trim arrays to a specific size using utility methods.
4. Counting Elements for Conditional Logic
Apply conditional logic based on the number of elements using the .length property.
Example:
if (arr.length > 0) {
System.out.println("Array is not empty.");
}
Also check: Strings in JAVA: Concepts, Examples, and Best Practices
Understanding Java array length is essential for writing safe and efficient code. It helps in looping, validation, copying, and applying conditions based on array size. Java provides multiple ways to find the length, and each method suits different scenarios. Whether you're handling static arrays or dynamic lists, knowing how to work with array length improves your programming skills. Keep practicing with real examples to master it.
No, array length in Java is fixed once the array is created. If you need a resizable structure, consider using collections like ArrayList. To change size, you must create a new array and copy data manually or use Arrays.copyOf().
Trying to access an element beyond the array’s length causes a java.lang.ArrayIndexOutOfBoundsException. Java arrays are zero-indexed, so valid indexes range from 0 to array.length - 1. Always validate the index before accessing elements to avoid this runtime exception.
In Java, .length is a property of arrays, not a method. Unlike String.length() or ArrayList.size(), you don’t use parentheses. It directly returns the total number of elements the array can hold.
Yes, you can use .length with multi-dimensional arrays. For example, arr.length gives the number of rows, and arr[0].length gives the number of columns in the first row. This is helpful when iterating over 2D arrays.
You can check if an array is empty by verifying its length:
if (arr.length == 0) { // empty array }
Note that this works only for non-null arrays. Always check for null before accessing .length to avoid NullPointerException.
Yes, you can use the Array.getLength() method from the java.lang.reflect package. It’s useful when dealing with arrays generically without knowing the type at compile time. It's commonly used in frameworks and utility libraries.
You can multiply the number of rows and columns:
int total = arr.length * arr[0].length;
This works only if the array is rectangular (all rows have the same length). For jagged arrays, use nested loops to count elements manually.
An uninitialized array (i.e., declared but not created) does not have any length. Trying to access its length will throw a NullPointerException. You must initialize the array using new before using .length.
Yes, enhanced for-each loops automatically use the array’s length internally to iterate. However, if you need the index or conditional logic based on length, a traditional for loop with array.length is more appropriate.
Yes. .length is a constant-time property access with no overhead. Methods like .size() might involve additional processing depending on the collection. When working with arrays, .length is the most efficient way to retrieve size.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.