EN
Java - default array values
0
points
In this article, we would like to show you what are default array values initialized by the compiler when you don’t assign values to array elements in Java.
Quick solution:
Datatype | Default value |
---|---|
int | 0 |
float | 0.0 |
double | 0.0 |
boolean | false |
String | null |
byte | 0 |
Practical example
In this example, we create one-element, not initialized arrays to see what is the default value for each type.
import java.util.Arrays;
public class Example {
private static int[] intArray = new int[1];
private static float[] floatArray = new float[1];
private static double[] doubleArray = new double[1];
private static boolean[] booleanArray = new boolean[1];
private static String[] stringArray = new String[1];
private static byte[] byteArray = new byte[1];
public static void main(String[] args) {
System.out.println("int: " + Arrays.toString(intArray));
System.out.println("float: " + Arrays.toString(floatArray));
System.out.println("double: " + Arrays.toString(doubleArray));
System.out.println("boolean: " + Arrays.toString(booleanArray));
System.out.println("String: " + Arrays.toString(stringArray));
System.out.println("byte: " + Arrays.toString(byteArray));
}
}
Output:
int: [0]
float: [0.0]
double: [0.0]
boolean: [false]
String: [null]
byte: [0]