Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default initialization of an array in Java?

People also ask

What is initialization of array in Java?

Java initialize array is basically a term used for initializing an array in Java. We know that an array is a collection of similar types of data. The array is a very important data structure used for solving programming problems. The word element is used for the values stored in different positions of the array.

What is default initialization in Java?

From Java Language Specification. Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10): For type byte, the default value is zero, that is, the value of (byte)0. For type short, the default value is zero, that is, the value of (short)0.

Are arrays in Java initialized to 0?

By default in Java, data types like int, short, byte, and long arrays are initialized with 0.


Everything in a Java program not explicitly set to something by the programmer, is initialized to a zero value.

  • For references (anything that holds an object) that is null.
  • For int/short/byte/long that is a 0.
  • For float/double that is a 0.0
  • For booleans that is a false.
  • For char that is the null character '\u0000' (whose decimal equivalent is 0).

When you create an array of something, all entries are also zeroed. So your array contains five zeros right after it is created by new.

Note (based on comments): The Java Virtual Machine is not required to zero out the underlying memory when allocating local variables (this allows efficient stack operations if needed) so to avoid random values the Java Language Specification requires local variables to be initialized.


From the Java Language Specification:

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
 - For type byte, the default value is zero, that is, the value of `(byte)0`.
 - For type short, the default value is zero, that is, the value of `(short)0`.
 - For type int, the default value is zero, that is, `0`.
 - For type long, the default value is zero, that is, `0L`.
 - For type float, the default value is positive zero, that is, `0.0f`.
 - For type double, the default value is positive zero, that is, `0.0d`.
 - For type char, the default value is the null character, that is, `'\u0000'`.
 - For type boolean, the default value is `false`.
 - For all reference types (§4.3), the default value is `null`.

JLS clearly says

An array initializer creates an array and provides initial values for all its components.

and this is irrespective of whether the array is an instance variable or local variable or class variable.

Default values for primitive types : docs

For objects default values is null.


According to java,

Data Type - Default values

byte - 0

short - 0

int - 0

long - 0L

float - 0.0f

double - 0.0d

char - '\u0000'

String (or any object) - null

boolean - false


Thorbjørn Ravn Andersen answered for most of the data types. Since there was a heated discussion about array,

Quoting from the jls spec http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5 "array component is initialized with a default value when it is created"

I think irrespective of whether array is local or instance or class variable it will with default values


Every class in Java have a constructor ( a constructor is a method which is called when a new object is created, which initializes the fields of the class variables ). So when you are creating an instance of the class, constructor method is called while creating the object and all the data values are initialized at that time.

For object of integer array type all values in the array are initialized to 0(zero) in the constructor method. Similarly for object of boolean array, all values are initialized to false.

So Java is initializing the array by running its constructor method while creating the object