What if you haven’t initialized an element of an array in Java ???

Kirusanthan Ravichandran
4 min readDec 28, 2022

--

In this short article we are going to know what will be the output for a non-declared array elements for the 8 data types in java. To Explain I have created 8 arrays in the size of 5 and initialized only three of them after that i am printing fourth element of each array. Please go through the codes and get the outputs yourself. After that read the conclusion you can get the actual thing.

  1. Byte Datatype
 //Byte Array
byte[] byteArray = new byte[5];

for (int i = 0; i < 3; i++) {
byteArray[i] = (byte) (i);
}
System.out.println(byteArray[3]);

The output will be 0. This is because the byte array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is 0.

2.Short Datatype

//Short Array
short[] shortArray = new short[5];

for (int i = 0; i < 3; i++) {
shortArray[i] = (short) (i);
}
System.out.println(shortArray[3]);

The output will be 0. This is because the short array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is 0.

3.Integer Datatype

//Integer Array
int[] intArray = new int[5];

for (int i = 0; i < 3; i++) {
intArray[i] = i ;
}
System.out.println(intArray[3]);

The output will be 0. This is because the Integer array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is 0.

4.Long Datatype

//Long Array
long[] longArray = new long[5];

for (int i = 0; i < 3; i++) {
longArray[i] = i + 1;
}
System.out.println(longArray[3]);

The output will be 0. This is because the Long Array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is 0.

5.Float Datatype

//Float Array
float[] floatArray = new float[5];

for (int i = 0; i < 3; i++) {
floatArray[i] = i+1/2;
}
System.out.println(floatArray[3]);

The output will be ‘0.0’ This is because the Float array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is ‘0.0’.

6.Double Datatype

//Double Array
double[] doubleArray = new double[5];

for (int j = 10000000,i=0; i < 3; i++,j++) {
doubleArray[i] = j;
}
System.out.println(doubleArray[3]);

The output will be ‘0.0’ This is because the Double array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is ‘0.0’.

7.Char Datatype

//Char Array
char[] charArray = new char[5];

for (int i = 0,k=66; i < 3; i++,k++) {
charArray[i] = (char) k;
}
System.out.println(charArray[3]);

The output will be ‘null’ This is because the Char array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any characters, so their default value is ‘null’.

8.Boolean Datatype

import java.util.Random;
//Boolean Array
Random random = new Random();
boolean[] booleanArray = new boolean[5];

for (int i = 0; i < 3; i++) {
booleanArray[i] = random.nextBoolean();
}
System.out.println(booleanArray[3]);

The output will be ‘false’ This is because the Boolean Array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any values, so their default value is ‘false’.

9.Object Array

Object[] objectArray = new Object[5];

for (int i = 0; i < 3; i++) {
objectArray[i] = new Object();
}
System.out.println(objectArray[3]);

The output of the code would be ‘null’. This is because the Object Array is initialized with a size of 5, but only 3 elements are assigned values. The elements at index 3 and 4 are not assigned any objects, so their default value is ‘null’.

Hope you can understand something from the above code segments and their outputs .

Finally we can come to a conclusion that when we failed to initiate an array the default value of data type (If it is an object the value will be null) will be initialized for the place of the elements.

In a Nutshell:-

Demonstration

--

--