HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Arrays

Arrays

Fixed-size arrays, index access, length, and iteration with for-each.

Simple enough for a 5-year-old to understand

Create an array and access elements

Example code
1
public class Main {
2
public static void main(String[] args) {
3
int[] numbers = {10, 20, 30};
int array.
Dataint array.
4
System.out.println(numbers[0]);
First element: 10
Output / InputFirst element: 10
5
System.out.println(numbers.length);
Size: 3 (without parentheses!)
Output / InputSize: 3 (without parentheses!)
6
}
7
}
DataOutput / Input
💡 Hint: Arrays are fixed-size; use ArrayList if it needs to grow.

About this topic

An array is the most fundamental data structure that holds values of the same type at a fixed length. In Java, the length of an array is determined at the moment it is created and CANNOT BE CHANGED: you cannot add an eleventh element to a ten-element array. This makes arrays fast but rigid.

When you create an array, the elements receive default values: numbers are zero, booleans are false, and objects are null. This requires care with object arrays — creating the array does not create the objects, you must assign each element separately.

In practice, an ArrayList is used instead of an array for data whose length changes. Arrays are preferred where performance is critical, in fixed-size tables, and for method parameters.

What you will learn

  • Defining arrays and initializing values
  • Getting the length with length
  • Accessing by index and that it starts from zero
  • Traversing arrays with loops and enhanced for
  • Two-dimensional arrays
  • Utility methods of the Arrays class

When to use?

An array is used when the number of elements is known in advance: the seven days of a week, a matrix, a buffer. You also see arrays in method parameters and the main method arguments.

If you need to add and remove elements, use an ArrayList; to grow an array, you have to create a new array and copy it, and ArrayList does this work for you.

Common mistakes

Wrong

int[] nums = new int[3];
nums[3] = 10;

The valid indices of a three-element array are 0, 1, and 2. Writing to index 3 throws the "ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3" error.

Correct

int[] nums = new int[3];
nums[2] = 10;

Wrong

String[] names = new String[2];
System.out.println(names[0].length());

Creating an object array does not create the objects; elements start as null. Calling a method on null gives a NullPointerException. You must assign each element before using it.

Correct

String[] names = new String[2];
names[0] = "Ada";
System.out.println(names[0].length());

Wrong

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);

Arrays are objects and assignment does not copy, it gives the same array a second name. Changes you make via b are also visible in a; the output becomes 99. For a copy, Arrays.copyOf should be used.

Correct

int[] a = {1, 2, 3};
int[] b = java.util.Arrays.copyOf(a, a.length);
b[0] = 99;
System.out.println(a[0]);

Frequently asked questions

Can I change the length of an array later?

No, array length in Java is fixed. If you need a larger array, you have to create a new array and copy the elements. The structure that does this automatically is ArrayList, and it is the right choice for data with changing lengths.

How do I print an array?

Printing directly with println prints the object address, not its contents. To see the contents, Arrays.toString is used; for two-dimensional arrays, Arrays.deepToString is needed. This is one of the most frequently searched items when debugging.

Should I use an array or an ArrayList?

An array if the number of elements is fixed and performance is critical. An ArrayList if you will add and remove elements. In everyday application code, ArrayList is used much more frequently; arrays mostly appear in low-level work and method signatures.

Summary

Java arrays are of fixed length and indices start at zero; out of bounds throws ArrayIndexOutOfBoundsException. Object arrays start with null elements and assignment does not copy the array — use ArrayList for data whose length changes.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code