Java un-initializing Array

We can create and initialize array using different statements, but what will be the content of the arrays if we cont initialize the array after creating it?
Let's create arrays of different data types and then print the content of each array to see what they contain.
Create Different Arrays
Create different arrays of primitive data types and one Object type (Integer)
byte rollNums1[] = new byte[10];
short rollNums2[] = new short[10];
int rollNums3[] = new int[10];
long rollNums4[] = new long[10];
float rollNums5[] = new float[10];
double rollNums6[] = new double[10];
char rollNums7[] = new char[10];
boolean rollNums8[] = new boolean[10];
Integer rollNums9[] = new Integer[10];
Iterate the arrays and print the content.
    byte rollNums1[] = new byte[10];
    System.out.println("\nbyte array");
    for (byte i : rollNums1) {
      System.out.print(i + ", ");
    }
    short rollNums2[] = new short[10];
    System.out.println("\nshort array");
    for (short i : rollNums2) {
      System.out.print(i + ", ");
    }
    
    int rollNums3[] = new int[10];
    System.out.println("\nint array");
    for (int i : rollNums3) {
      System.out.print(i + ", ");
    }
    
    long rollNums4[] = new long[10];
    System.out.println("\nlong array");
    for (long i : rollNums4) {
      System.out.print(i + ", ");
    }
    
    float rollNums5[] = new float[10];
    System.out.println("\nfloat array");
    for (float i : rollNums5) {
      System.out.print(i + ", ");
    }
    
    double rollNums6[] = new double[10];
    System.out.println("\ndouble array");
    for (double i : rollNums6) {
      System.out.print(i + ", ");
    }
    
    
    char rollNums7[] = new char[10];
    System.out.println("\nchar array");
    for (char i : rollNums7) {
      System.out.print(i + ", ");
    }
    
    boolean rollNums8[] = new boolean[10];
    System.out.println("\nboolean array");
    for (boolean i : rollNums8) {
      System.out.print(i + ", ");
    }
    
    Integer rollNums9[] = new Integer[10];
    System.out.println("\nInteger array");
    for (Integer i : rollNums9) {
      System.out.print(i + ", ");
    }
Output
byte array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
short array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
int array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
long array 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
float array 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
double array 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
char array , , , , , , , , , , 
boolean array false, false, false, false, false, false, false, false, false, false, 
Integer array null, null, null, null, null, null, null, null, null, null,

Conclusion
  • If the array is just created and not initialized with data then the content of the arrays depends on the data type.
  • For byte, short, int, long it is 0
  • For float and double it is 0.0
  • for char it is '' (empty character)
  • for boolean it is false
  • for Integer or any other object, it is null
  • The value of the array depends on the type of the array and a default value for the respective data types.

References

Array Initialize

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Each item in the array can be accessed with its index very efficiently. In this post we examine different ways to create and initialize arrays in java
Creating array of specific size and populating using for loop
We create a new int array of size 10 and set values using for loop.
//initialize array of size 10 of type int
int rollNums[] = new int[10];
//iterate from 0 to the last index of the array and set values
for (int i = 0; i < rollNums.length; i++) {
      rollNums[i] = i + 1;
}
Assigning value while creating the array.
Here we create a new int array and pass the values in the curly braces.
int rollNums[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Assigning value while creating the array without specifying array type.
Even shorter versions just assign values in the curly braces.
int rollNums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Using Arrays.setAll (Lambda)
First, create an int array of size 10, then using setAll to set the value of the corresponding indices. setAll takes IntUranaryOperator as the second argument. IntUranaryOperator is a functional interface and we can use lambda expressions to implement it.
int rollNums[] = new int[10];
Arrays.setAll(rollNums, p -> p + 1);
Using Arrays.setAll (Anonymous Class)
Same as above but rather than lambda expression implementing it as Anonymous class and implementing applyAsInt method.
int rollNums[] = new int[10];
Arrays.setAll(rollNums, new IntUnaryOperator () {
      @Override
      public int applyAsInt(int operand) {
        return operand + 1;
 }} );
Arrays.copyOf
We can also copy contents of an existing array to a different array of the same type.
int rollNums2[] = Arrays.copyOf(rollNums, rollNums.length);
Summary
  • We can create an array in java using a new operator and specify size while creating.
  • We can also specify the content of the array and java will create an array of sufficient size.
  • When we create an array and does not initialize then the content of the array is null if the type of the array is of type object
  • and 0 if the type is int.