Array default value in java

When we are creating a new array, the elements in the array will automatically be initialized by their default values. After construction of array, a new array of in heap memory according to the type of array. In this article, we will discuss what is Array default value in java.

Below are the default assigned values.

  • boolean: false
  • int : 0
  • double : 0.0
  • String: null
  • User-Defined Type: null
class ClassOne 
{
   public static void main(String[] args) 
   {
	
	 // Declaring an int type array
	 int intArray[] = new int[2]; 
	 // Print all elements of intArray
	 for (int i = 0; i < intArray.length; i++) 
	   System.out.println("Elements of intArray = " + intArray[i]);
	 
	// Declaring an boolean type array
	boolean[] booleanArray = new boolean[2]; 
	// Print all elements of booleanArray
	for (int i = 0; i < booleanArray.length; i++) 
	   System.out.println("Elements of booleanArray = " + booleanArray[i]);
	
	// Declaring an String type array
	String[] stringArray = new String[2]; 
	// Print all elements of stringArray
	for (int i = 0; i < stringArray.length; i++) 
	   System.out.println("Elements of stringArray = " + stringArray[i]);
  }
}

Output: Elements of intArray = 0
Elements of intArray = 0
Elements of booleanArray = false
Elements of booleanArray = false
Elements of stringArray = null
Elements of stringArray = null

Leave a Comment