Initialize Array in Java

In Java, an array is a container that holds a predetermined number of elements of a single type. To create an array in Java, you start by declaring it by specifying the element type followed by square brackets. When an instance is made, the length or capacity of the array becomes immutable. To instantiate an array, you have two options: use the new keyword followed by a type and assigned size or directly allocate a set of elements separated by commas and enclosed in curly braces. When the new keyword is used, array components default to their underlying values, be it 0 for numeric arrays, false for Boolean arrays, or null for object arrays. Conversely, suppose explicit values must be specified during declaration. In that case, the curly brace methodology comes to the fore to bypass the need to declare the size since the number of elements provided implicitly determines it. In this Java Array Initialization Example, we have created two arrays and filled them differently. Click Execute to run the Java Array Example online and see the result.
Initialize Array in Java Execute
public class ArrayInitialization {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie", "David"};

        System.out.println("Names array:");
        for(String name : names) {
        int[] numbers = new int[5];
        System.out.println("\nNumbers array:");
        for(int num : numbers) {

Updated: Viewed: 69 times