Learning arrays in Java is one of the most essential and frequently used skills in any Java tutorial for beginners — arrays let you store multiple values of the same type in a single variable, making it easy to work with lists of data like scores, names, passwords, pixels, or database results. In 2025–2026, arrays remain a core building block in Java — even modern collections (ArrayList, HashMap) are built on top of arrays internally. Understanding arrays in Java is crucial for writing efficient, readable, and performant code.

In this complete arrays in Java tutorial, you’ll learn:

  • How to create and initialize arrays
  • Access, modify, and loop through elements
  • Use the powerful Arrays class (sort, search, copy, fill, equals)
  • Work with arrays of primitives vs objects
  • Secure password storage with char[] (why better than String)
  • Common mistakes, best practices & modern tips

All examples are tested in jshell (Java’s REPL) — copy-paste to see instant results on Java 21 (LTS in 2025–2026).

Prerequisites

  • Java 11+ installed (preferably 17 or 21 LTS)
  • Terminal + jshell (type jshell to start)
  • Basic knowledge of variables, data types, loops & conditionals

1. Creating & Initializing Arrays in Java

There are three main ways to create arrays in Java:

Way 1 – Declare size, fill later

				
					int[] scores = new int[5];   // creates array of 5 zeros
				
			

Way 2 – Declare & initialize with values

				
					String[] names = {"Zain", "Ali", "Sara", "Ahmed"};
				
			

Way 3 – Declare first, initialize later

				
					char[] password;
password = new char[] {'s', 'e', 'c', 'r', 'e', 't'};
				
			

Secure password example (why char[] > String):

				
					char[] password = {'m', 'y', 'P', 'a', 's', 's', '1', '2', '3'};
// Later: overwrite to erase from memory
java.util.Arrays.fill(password, ' ');   // safer than String
				
			

2. Accessing & Modifying Array Elements

Arrays use zero-based indexing (first element = [0]).

				
					String[] fruits = {"Apple", "Banana", "Cherry"};

System.out.println(fruits[0]);   // Apple
System.out.println(fruits[1]);   // Banana

fruits[2] = "Mango";             // change Cherry → Mango
System.out.println(fruits[2]);   // Mango
				
			

Get array length:

				
					System.out.println(fruits.length);   // 3
				
			

3. Looping Through Arrays in Java

Classic for loop (with index):

				
					for (int i = 0; i < fruits.length; i++) {
    System.out.println("Fruit " + (i+1) + ": " + fruits[i]);
}
				
			

foreach loop (enhanced for – recommended):

				
					for (String fruit : fruits) {
    System.out.println("Fruit: " + fruit);
}
				
			

Output:

Fruit: Apple
Fruit: Banana
Fruit: Mango

4. Using the Arrays Class – Powerful Helper for Arrays in Java

Import: import java.util.Arrays;

Common methods:

  • Arrays.toString(array) – print readable version
				
					System.out.println(Arrays.toString(fruits));
// [Apple, Banana, Mango]
				
			
  • Arrays.sort(array) – sort in natural order
				
					int[] nums = {5, 2, 8, 1, 9};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));   // [1, 2, 5, 8, 9]
				
			
  • Arrays.binarySearch(sortedArray, value) – fast search (array must be sorted)
				
					int index = Arrays.binarySearch(nums, 8);
System.out.println("Found 8 at index: " + index);   // 3
				
			
  • Arrays.copyOf(array, newLength) – copy & resize
				
					int[] bigger = Arrays.copyOf(nums, 7);   // pads with 0s
				
			
  • Arrays.fill(array, value) – fill all elements
				
					Arrays.fill(password, ' ');   // securely erase password
				
			
  • Arrays.equals(array1, array2) – deep comparison
				
					char[] pass1 = {'a', 'b', 'c'};
char[] pass2 = {'a', 'b', 'c'};
System.out.println(Arrays.equals(pass1, pass2));   // true
				
			

5. Multidimensional Arrays in Java

2D array (matrix):

				
					int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

System.out.println(matrix[1][2]);   // 6
				
			

Loop through 2D array:

				
					for (int[] row : matrix) {
    for (int num : row) {
        System.out.print(num + " ");
    }
    System.out.println();
}
				
			

6. Best Practices & Modern Tips (2025–2026)

  • Prefer collections (ArrayList<String>) over arrays for dynamic size
  • Use Arrays.toString() or Arrays.deepToString() for debugging
  • Avoid fixed-size arrays when size is unknown — use ArrayList
  • Always check bounds: if (i < array.length)
  • Use var (Java 10+) for cleaner declarations:
				
					var scores = new int[5];
				
			
  • Secure sensitive data (passwords) with char[] + overwrite
  • Use Arrays.stream(array) for functional-style processing (Java 8+)

Arrays in Java – FAQ (2025–2026)

  1. How do I create arrays in Java?
    int[] nums = new int[5]; or String[] names = {“Ali”, “Sara”};
  2. How do I loop through arrays in Java?
    Best: for (String name : names) { … } (foreach)
  3. How do I sort arrays in Java?Arrays.sort(myArray);
  4. Why use char[] for passwords in Java?
    Can overwrite memory (Arrays.fill) — String is immutable
  5. What’s the length of an array in Java?
    array.length (no parentheses — it’s a field)

Summary

You now fully understand arrays in Java: creation, initialization, access/modify, looping, Arrays class methods, multidimensional arrays, security, and best practices.

Mastering arrays in Java gives you the foundation for lists, matrices, data processing, algorithms, and almost every real Java program.