Java Programming Handbook

    Java Arrays

    An array in Java is a data structure that allows us to store multiple values of the same type in a single variable. Instead of declaring separate variables for each value, we can use an array to efficiently manage data.

    In this blog, we will cover:

    • What is an Array?
    • How to Declare an Array
    • How to Initialize an Array
    • Accessing and Modifying Array Elements
    • Array Length
    • Looping Through an Array
    • Example Programs with Explanations
    • Limitations of Arrays

    What is an Array?#

    An array is a collection of elements stored contiguously in memory. All elements in an array must be of the same data type.

    For example, if we want to store the marks of 5 students, instead of declaring multiple variables:

    int mark1, mark2, mark3, mark4, mark5;

    We can use an array:

    int[] marks = new int[5];

    This creates an array that can store 5 integer values.

    How to Declare an Array#

    In Java, we can declare an array in two ways:

    1. Using the new keyword (Preferred)#

    int[] numbers = new int[5]; // Declares an array of size 5

    What happens when you declare an array with the new keyword:

    • You are telling Java to create a new array object of type int and size 5.
    • Since int is a primitive type, all elements in the array are automatically initialized to 0 (the default value for int).

    Internally:

    • Memory is allocated for 5 ints.
    • Values are: [0, 0, 0, 0, 0].

    2. Using Direct Initialization#

    int[] numbers = {10, 20, 30, 40, 50}; // Array with 5 elements

    What’s going on:

    • This is a shorthand syntax for declaring and initializing the array in one step.
    • Java automatically infers the size of the array based on the number of values provided (5 here).

    Internally:

    • A new array of size 5 is created and filled immediately with the given values.

    How to Initialize an Array#

    We can initialize an array in different ways:

    1. Default Initialization#

    When an array is declared using new, its elements are automatically initialized to:

    • 0 for numeric types (int, double, float, etc.).
    • false for boolean type.
    • null for object types (String, Integer, etc.).
    class Main { public static void main(String[] args) { int[] numbers = new int[5]; // All elements initialized to 0 // Print default values System.out.println(numbers[0]); // Output: 0 } }

    Output:

    0

    2. Explicit Initialization#

    int[] numbers = {10, 20, 30, 40, 50}; // Elements explicitly initialized

    Accessing and Modifying Array Elements#

    Array elements are accessed using index numbers, starting from 0.

    class Main { public static void main(String[] args) { int[] num = {5, 10, 15, 20}; // Accessing elements System.out.println("First element: " + num[0]); // Modifying elements num[1] = 50; System.out.println("Updated second element: " + num[1]); } }

    Output:

    First element: 5 Updated second element: 50

    Array Length#

    To find the number of elements in an array, we use .length.

    class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; // Get array length System.out.println("Array Length: " + numbers.length); } }

    Output:

    Array Length: 5

    Looping Through an Array#

    We can iterate over an array using:

    1. for Loop#

    class Main { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }

    Output:

    Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50

    2. Enhanced for Loop (for-each)#

    A simpler way to iterate through an array is using the enhanced for loop.

    class Main { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; for (int num : numbers) { System.out.println("Number: " + num); } } }

    Output:

    Number: 10 Number: 20 Number: 30 Number: 40 Number: 50

    Example Programs#

    Example 1: Find the Largest Element in an Array#

    class Main { public static void main(String[] args) { int[] numbers = {15, 42, 7, 98, 30}; int max = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } } System.out.println("Largest Number: " + max); } }

    Output:

    Largest Number: 98

    Example 2: Calculate the Sum of Array Elements#

    class Main { public static void main(String[] args) { int[] numbers = {5, 10, 15, 20, 25}; int sum = 0; for (int num : numbers) { sum += num; } System.out.println("Sum of Elements: " + sum); } }

    Output:

    Sum of Elements: 75

    Limitations of Arrays#

    1. Fixed Size: Once declared, an array’s size cannot be changed.
    2. Same Data Type: Arrays can only store elements of one data type.
    3. Inefficient Insertion/Deletion: Adding/removing elements in the middle of an array is slow.

    Conclusion#

    In this blog, we have learned:

    • What arrays are and why they are useful.
    • How to declare and initialize arrays in Java.
    • How to access and modify array elements.
    • Finding the length of an array using .length.
    • Looping through arrays using for and for-each loops.
    • Example programs to find the largest number and sum of elements.
    • Limitations of arrays in Java.

    Arrays provide a simple and efficient way to store multiple values. However, due to their fixed size, developers often use ArrayLists for more flexibility (which we will discuss in future blogs).

    Last updated on Apr 09, 2025