Array Length

Su Suriya Ravichandran Updated 14 Nov 2026
8 min read ·Lesson 65 of 306

Every Java array knows how many elements it holds, available through its length field. The length is set when the array is created and can never change. Using length correctly is what keeps loops within bounds, makes code work for arrays of any size, and avoids the most common array error, ArrayIndexOutOfBoundsException.

Reading the Length

int[] scores = {72, 85, 90, 64};
String[] empty = new String[0];
double[] buffer = new double[1024];

System.out.println(scores.length);   // 4
System.out.println(empty.length);    // 0
System.out.println(buffer.length);   // 1024

length is a field, not a method, so it has no parentheses. It is final: assigning to it does not compile.

scores.length = 10;      // compile error: cannot assign a value to final variable length

length reports the capacity of the array: the number of slots, not the number of slots you have filled with meaningful values. new int[5] has length 5 even though every element is 0.

length, length(), and size()

Three similar names are easy to mix up:

TypeHow to get the countKind
Array (int[], String[], ...)arr.lengthField
Stringstr.length()Method
Collection (List, Set, Map)list.size()Method
String[] words = {"java", "bash"};
System.out.println(words.length);        // 2: number of strings in the array
System.out.println(words[0].length());   // 4: number of characters in "java"

List<String> list = List.of("a", "b", "c");
System.out.println(list.size());         // 3

The inconsistency is historical: arrays are built into the language, while String and collections are ordinary classes with methods.

Indexes Run from 0 to length − 1

An array of length n has valid indexes 0 through n - 1:

length = 5
index:  0    1    2    3    4
      [ a ][ b ][ c ][ d ][ e ]
                          └── last index = length - 1
char[] letters = {'a', 'b', 'c', 'd', 'e'};
char first = letters[0];
char last = letters[letters.length - 1];     // 'e'
char middle = letters[letters.length / 2];   // 'c'

Using letters[letters.length] is a classic off-by-one error:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

The exception message states both the attempted index and the actual length, which usually points directly at the bug.

Using length in Loops

Writing length in the loop condition, rather than a hard-coded number, keeps the loop correct for any array:

int[] values = {4, 8, 15, 16, 23, 42};

int sum = 0;
for (int i = 0; i < values.length; i++) {      // < , not <=
    sum += values[i];
}
System.out.println(sum);                        // 108
PatternCovers
for (int i = 0; i < a.length; i++)Every index, forward
for (int i = a.length - 1; i >= 0; i--)Every index, backward
for (int i = 1; i < a.length; i++)Every index except the first (for comparing with a[i - 1])
for (int i = 0; i < a.length - 1; i++)Every index except the last (for comparing with a[i + 1])

The last two forms are used when each step looks at a neighbor. Getting the bound right ensures the neighbor index stays valid.

Empty Arrays

An array of length 0 is legal and useful. It represents "no elements" without using null:

int[] none = new int[0];
int[] alsoNone = {};

for (int v : none) {
    System.out.println(v);    // never runs
}
System.out.println(none.length);   // 0

Code that loops using length handles empty arrays automatically. Code that reads a[0] without checking does not:

static int max(int[] a) {
    if (a.length == 0) {
        throw new IllegalArgumentException("array is empty");
    }
    int m = a[0];
    for (int i = 1; i < a.length; i++) m = Math.max(m, a[i]);
    return m;
}

Methods that return arrays should return an empty array rather than null when there is nothing to return. Callers can then loop over the result without a null check:

static String[] tagsFor(String post) {
    if (post.isBlank()) {
        return new String[0];
    }
    return post.split("\\s+");
}

null versus empty

null means there is no array at all. Reading length on it throws:

int[] data = null;
System.out.println(data.length);    // NullPointerException

Check for null first when an array may be absent: if (data == null || data.length == 0).

The Length Is Fixed

An array cannot grow or shrink. "Resizing" means creating a new array and copying the elements:

import java.util.Arrays;

int[] small = {1, 2, 3};
int[] larger = Arrays.copyOf(small, 6);      // [1, 2, 3, 0, 0, 0]
int[] shorter = Arrays.copyOf(small, 2);     // [1, 2]

small = Arrays.copyOf(small, small.length * 2);   // reassign the variable to the new array

The original array object is unchanged; the variable now points to a different, larger array.

Tracking how many slots are used

When an array is used as a buffer, its length (capacity) and the number of meaningful elements (size) differ. Code must track the size separately:

public class IntBuffer {
    private int[] data = new int[4];
    private int size = 0;                 // how many slots are actually in use

    public void add(int value) {
        if (size == data.length) {        // full: grow by doubling
            data = Arrays.copyOf(data, data.length * 2);
        }
        data[size++] = value;
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index " + index + ", size " + size);
        }
        return data[index];
    }

    public int size() { return size; }
    public int capacity() { return data.length; }
}
IntBuffer buf = new IntBuffer();
for (int i = 1; i <= 5; i++) buf.add(i * 10);
System.out.println(buf.size() + " / " + buf.capacity());   // 5 / 8

This is essentially how ArrayList works internally: an array with spare capacity, a separate size counter, and growth by copying when full. Doubling the capacity keeps the average cost of each add constant, because copies become increasingly rare as the array grows.

Length of Multidimensional Arrays

A two-dimensional array is an array of arrays. Its length is the number of rows; each row has its own length:

int[][] grid = new int[3][4];

System.out.println(grid.length);       // 3: rows
System.out.println(grid[0].length);    // 4: columns in row 0

Rows do not have to be the same length ("jagged" arrays):

int[][] triangle = {
    {1},
    {1, 1},
    {1, 2, 1},
    {1, 3, 3, 1}
};

for (int r = 0; r < triangle.length; r++) {
    for (int c = 0; c < triangle[r].length; c++) {   // use THIS row's length
        System.out.print(triangle[r][c] + " ");
    }
    System.out.println();
}

Output:

1 
1 1 
1 2 1 
1 3 3 1 

Using triangle[0].length for every row would read past the end of the shorter rows. Always use arr[r].length for the inner loop.

Limits on Array Length

  • The length is an int, so the theoretical maximum is Integer.MAX_VALUE (about 2.1 billion). In practice the JVM reserves a few header slots, and very large arrays also require enough heap memory.
  • A negative length throws NegativeArraySizeException.
  • Requesting more memory than the heap allows throws OutOfMemoryError:
long[] huge = new long[500_000_000];   // needs about 4 GB; fails on a smaller heap

When reading a length from external input (a file header, a network message, a user), validate it before allocating, so a malformed or malicious value cannot make the program allocate an enormous array.

length with Varargs and main

Varargs parameters and main's args are ordinary arrays, so length tells you how many arguments were passed:

public static void main(String[] args) {
    if (args.length < 2) {
        System.err.println("Usage: java Copy <source> <target>");
        System.exit(1);
    }
    System.out.println("Copying " + args[0] + " to " + args[1]);
}

static double average(double... values) {
    if (values.length == 0) return 0;
    double sum = 0;
    for (double v : values) sum += v;
    return sum / values.length;
}

Checking args.length before reading args[0] prevents an ArrayIndexOutOfBoundsException when the program is run without arguments.

Practical Example: Paginating Results

import java.util.Arrays;

public class Pager {
    static String[] page(String[] items, int pageNumber, int pageSize) {
        if (pageSize <= 0) throw new IllegalArgumentException("pageSize must be positive");
        int totalPages = (items.length + pageSize - 1) / pageSize;   // ceiling division
        if (pageNumber < 1 || pageNumber > Math.max(totalPages, 1)) {
            return new String[0];
        }
        int from = (pageNumber - 1) * pageSize;
        int to = Math.min(from + pageSize, items.length);           // last page may be short
        return Arrays.copyOfRange(items, from, to);
    }

    public static void main(String[] args) {
        String[] products = {"Pen", "Pencil", "Eraser", "Ruler", "Stapler", "Tape", "Glue"};
        int size = 3;
        int pages = (products.length + size - 1) / size;

        for (int p = 1; p <= pages; p++) {
            String[] onPage = page(products, p, size);
            System.out.println("Page " + p + "/" + pages + " (" + onPage.length + " items): "
                    + Arrays.toString(onPage));
        }
        System.out.println("Page 9: " + Arrays.toString(page(products, 9, size)));
    }
}

Output:

Page 1/3 (3 items): [Pen, Pencil, Eraser]
Page 2/3 (3 items): [Ruler, Stapler, Tape]
Page 3/3 (1 items): [Glue]
Page 9: []

Every calculation is driven by items.length. Ceiling division gives the number of pages, so 7 items in pages of 3 need 3 pages. Math.min(from + pageSize, items.length) keeps the last page from running past the end of the array, which is why page 3 contains only one item. An out-of-range page returns an empty array, not null, so the caller can print or loop over it safely. The same arithmetic underlies pagination in web applications and database queries.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Array Length and get answers drawn from it.

Signed-in readers only.