Languages
[Edit]
EN

Java - default array values

0 points
Created by:
Warren-X
323

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:

DatatypeDefault value
int0
float0.0
double0.0
booleanfalse
Stringnull
byte0

 

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]

Cross technology - default array values

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join