Java ‘int’ array examples (declaring, initializing, populating)

Java array FAQ: How do you create an array of Java int values (i.e., a Java “int array”)?

Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.

1) Declare a Java int array with initial size; populate it later

If you know the desired size of your array, and you’ll be adding elements to your array some time later in your code, you can define a Java int array and then populate it using this syntax:

// (1) create a java int array
int[] intArray = new int[3];

// (2) some time later ... assign elements to the array
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;

// (3) print our java int array
for (int i=0; i<intArray.length; i++) {
  System.out.println(intArray[i]);
}

2) Declare an int array as you populate its elements

Depending on your needs you can also create an int array with initial elements like this:

// (1) define your java int array
int[] intArray = new int[] {4,5,6,7,8};

// (2) print the java int array
for (int i=0; i<intArray.length; i++) {
  System.out.println(intArray[i]);
}

3) A complete Java int array example

Sometimes it helps to see source code used in a complete Java program, so the following program demonstrates the different Java int array examples.

The method named intArrayExample shows the first example. Then, to demonstrate the similarity between an int array and a String array syntax, the method named stringArrayExample shows how a String array is typically created and populated. Finally, the method named intArrayExample2 shows a second int array example (as shown above):

/**
 * Demonstrates several Java array examples, including a
 * Java int array, and a Java String array.
 * Created by Alvin Alexander, http://alvinalexander.com.
 */
public class JavaIntArrayExample
{

  public static void main(String[] args) {
    new JavaArrayExample();
  }

  public JavaArrayExample() {
    intArrayExample();
    stringArrayExample();
    intArrayExample2();
  }

  /**
   * Create an int array, then populate the array, 
   * and finally print each element in the int array.
   */
  private void intArrayExample() {
    int[] intArray = new int[3];
    intArray[0] = 1;
    intArray[1] = 2;
    intArray[2] = 3;
    System.out.println("intArray output");
    for (int i=0; i<intArray.length; i++) {
      System.out.println(intArray[i]);
    }
  }

  /**
   * Create a String array, then populate the array, 
   * and finally print each element in the int array.
   */
  private void stringArrayExample() {
    String[] stringArray = new String[3];
    stringArray[0] = "a";
    stringArray[1] = "b";
    stringArray[2] = "c";
    System.out.println("stringArray output");
    for (int i=0; i<stringArray.length; i++) {
      System.out.println(stringArray[i]);
    }
  }

  /**
   * Create a Java int array and populate it in one step.
   * Then get the array length and print each element in the array.
   */
  private void intArrayExample2() {
    int[] intArray = new int[] {4,5,6,7,8};
    System.out.println("intArray output (version 2)");
    for (int i=0; i<intArray.length; i++) {
      System.out.println(intArray[i]);
    }
  }

}

As a quick summary, if you were looking for examples of the Java int array syntax, I hope these examples are helpful.