Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Return an Array in Java?
Next article icon

How to Find Length or Size of an Array in Java?

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.

In this article, we will learn:

  • How to find the length of an array using the length property
  • Difference between length, length() and size() methods
  • Various approaches to find the array length, including loops and Stream API

Different Ways to Find the Length or Size of an Array

1. Using the length Property (Best and Easiest Way)

The length property is the most common way to find the size of an array in Java. It gives the exact number of elements present in the array.

Example: This example demonstrates how to find the length (size) of an array using the length property.

Java
// Finding the length of an Array public class Geeks {        public static void main(String[] args)     {         // Here arr is the         // array name of int type         int[] arr = new int[4];          System.out.println("The size of the Array is " + arr.length);     } } 

Output
The size of the Array is 4 

Time complexity: O(1) constant time

Note:

  • length is a property not a method.
  • length property works for all array types (int[] , String[], double[]) etc.


2. Finding Array Length Using a For Loop (Manual Counting)

When iterating over an array, we can also determine its size manually.

Example: This example, demonstrates how to use for each loop to calculate the length of an array.

Java
// Java program to demonstrate for loop // to calculate length of all type of Arrays import java.util.*;  public class Geeks {     public static void main(String[] arg) {          int[] arr = { 1, 2, 3, 4, 5, 6, 7 };       	int c = 0;                	for (int i : arr)             c++;                	System.out.println("The Size of the array is " + c);              } } 

Output
The Size of the array is 7 

Time Complexity: O(n) slower than length

Note: This approach is not recommended in real world code.


3. Finding Array Length Using Java 8 Stream API

Stream API was introduced in Java 8 and because of it we can perform various operations on arrays using functional programming. The Stream class provide count() method which is used to count the number of elements in an array.

Example: This example, demonstrates using Arrays.stream() with count() to calculate the length of an array.

Java
// Java program to demonstrate Stream.count() // method to calculate length of an array import java.util.*;  public class Geeks {     public static void main(String[] argv)     {        	// Creating Array and Populating them         int[] arr = { 1, 2, 3, 4, 5 };          // calculating the length of the arrays         long e = Arrays.stream(arr).count();              // print the length of the arrays         System.out.println("The size of the array is " + e);     } } 

Output
The size of the array is 5 

Time Complexity: O(n)

Note: Avoid Arrays.stream(arr).count() for arrays because it forces unnecessary iteration.


length vs length() vs size()

The difference between length, length() and size() is listed below:

Feature

Used For

Example

length

It is used for arrays to calculate the number of elements

arr.length

length()

It is used for strings to calculate the number of characters in the string

s.length()

size()

It is used for collections like ArrayList or List to calculate the number of elements

list.size()



Next Article
How to Return an Array in Java?

C

code_r
Improve
Article Tags :
  • Java
  • Java-Array-Programs
  • Java-Arrays
Practice Tags :
  • Java

Similar Reads

  • Java Program to Find the Length/Size of an ArrayList
    Given an ArrayList in Java, the task is to write a Java program to find the length or size of the ArrayList. Examples: Input: ArrayList: [1, 2, 3, 4, 5] Output: 5 Input: ArrayList: [geeks, for, geeks] Output: 3 ArrayList - An ArrayList is a part of the collection framework and is present in java.uti
    2 min read
  • How to Get the Size of an Array in JavaScript
    To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntax const a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
    2 min read
  • How to Return an Array in Java?
    An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of
    5 min read
  • Difference between length of Array and size of ArrayList in Java
    Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java. Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializ
    2 min read
  • How to Calculate Size of Object in Java?
    In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
    2 min read
  • How to find length of matrix in R
    In this article, we will examine various methods to find the length of a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data structure that is a collection of rows and columns. A matrix can able to contain data of various types such as numeric, characters, and
    4 min read
  • How to Declare an Array in Java?
    In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif
    3 min read
  • How to Add an Element to an Array in Java?
    In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the New Different Ways to Add an Element to an ArrayThere are two different approaches we
    3 min read
  • ArrayDeque size() Method in Java
    The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque. Syntax: Array_Deque.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Deque.
    2 min read
  • Find max or min value in an array of primitives using Java
    A simple method is traverse the array. We initialize min and max as first elements and then compare every element (Starting from 2nd) with current min and max. [GFGTABS] Java // Java program to find min & max in int[] // array without asList() public class MinNMax { public static void main(Strin
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences