0
0
Javaprogramming~15 mins

Array declaration and initialization in Java

Choose your learning style8 modes available
menu_bookIntroduction

Arrays help you store many values in one place. This makes it easy to keep and use groups of data together.

When you want to keep a list of student names in a class.
When you need to store daily temperatures for a week.
When you want to save scores of players in a game.
When you want to hold multiple colors for a drawing app.
When you want to process a fixed number of inputs together.
regular_expressionSyntax
Java
public class ArrayExample {
    public static void main(String[] args) {
        // Declare an array of integers
        int[] numbers;

        // Initialize the array with size 5
        numbers = new int[5];

        // Or declare and initialize at the same time
        int[] scores = new int[3];

        // Declare and initialize with values
        String[] names = {"Alice", "Bob", "Charlie"};
    }
}

You declare an array by specifying the type followed by square brackets [].

You initialize an array by creating it with a size or by giving values directly.

emoji_objectsExamples
line_end_arrow_notchThis creates an empty array with zero length.
Java
int[] emptyArray = new int[0];
line_end_arrow_notchThis creates an array with one element and sets that element to 10.
Java
int[] singleElementArray = new int[1];
singleElementArray[0] = 10;
line_end_arrow_notchThis creates and fills the array with three fruit names.
Java
String[] fruits = {"Apple", "Banana", "Cherry"};
line_end_arrow_notchThis is another way to declare and initialize an array with values.
Java
double[] prices = new double[]{2.5, 3.0, 4.75};
code_blocksSample Program

This program shows how to declare arrays, print their default values, assign new values, and print them again. It also shows how to declare and initialize a String array with values directly.

Java
public class ArrayDeclarationInitialization {
    public static void main(String[] args) {
        // Declare and initialize an integer array with size 4
        int[] ages = new int[4];

        // Print default values
        System.out.println("Ages before assignment:");
        for (int index = 0; index < ages.length; index++) {
            System.out.println("ages[" + index + "] = " + ages[index]);
        }

        // Assign values to the array
        ages[0] = 21;
        ages[1] = 25;
        ages[2] = 30;
        ages[3] = 28;

        // Print values after assignment
        System.out.println("\nAges after assignment:");
        for (int index = 0; index < ages.length; index++) {
            System.out.println("ages[" + index + "] = " + ages[index]);
        }

        // Declare and initialize a String array with values
        String[] cities = {"New York", "London", "Tokyo"};

        System.out.println("\nCities array:");
        for (int index = 0; index < cities.length; index++) {
            System.out.println("cities[" + index + "] = " + cities[index]);
        }
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Arrays have a fixed size once created. You cannot change their length later.

line_end_arrow_notch

Default values for int arrays are 0, for objects like String they are null.

line_end_arrow_notch

Accessing an index outside the array size causes an error (ArrayIndexOutOfBoundsException).

list_alt_checkSummary

Arrays store multiple values of the same type in one variable.

Declare arrays with type[] and initialize with size or values.

Use loops to access or change array elements by their index.