What is default array values in Java?

If we don't assign a value to an array element and try to access it, the compiler won't produce an error like it would with a simple variable. Instead, it provides non-junk value.

In Java, when an array is created, each element of the array is assigned a default value based on its data type. This default value is the value that the element will hold until a different value is explicitly assigned to it.

The default value for numeric types (byte, short, int, long, float, and double) is 0. For boolean types, the default value is false. For the char type, the default value is '\u0000' (null character). For object references, the default value is null.

For example, if you create an array of integers: 

 

int[] nums = new int[5];

Each element in the array will be initialized with the default value of 0. So nums[0] will be 0, nums[1] will be 0, and so on.

Similarly, if you create an array of booleans: 

 

boolean[] flags = new boolean[3];

Each element in the array will be initialized with the default value of false. So flags[0] will be false, flags[1] will be false, and so on.

It's important to be aware of the default values of array elements, especially when working with uninitialized arrays. If you try to access an element of an uninitialized array, you will get the default value for that data type.