How to initialize an array with zero values in Java

This article will look at how to fill up Java array with default values effectively on an array creation.

Overview

Arrays are essential cornerstones of every modern programming language. On a specific occasion, you need to initialize an array with zero values. This article will take you through the pros and cons of the various way how to initialize Java array with default zero values.

Introduction to Java data types

In Java we have several different data types. Majority of data types are called primitive data types. To this group belong byte, short, int, long, float, double, boolean and char.

For more information about Java’s data type, please read our article about primitive data types.

Speaking about Java’s data type, there is also a general notation of different data type represented by referencing the object’s type. We are talking about object references where the type of the object is classified by object’s referencing type. Sound confusing? If you are new to Java let me point you out to one of the most asked Java questions Is Java pass by value or pass by reference?

Java data type default values

Let’s take a look what is initial (default) value in Java language for any variable initialization.

  • The default value for type byte is zero. That means in byte the value of (byte) is 0.
  • The default value for type short is zero. That means in the short the value of (short) is 0.
  • The default value for type int is zero. That means in int the value of (int) is 0.
  • The default value for type long is zero. That means in long the value of (long) is 0L.
  • The default value for type float is positive zero. That means in float the value of (float) is 0.0f.
  • The default value for type double is positive zero. That means in double the value of (double) is 0.0d.
  • The default value for type boolean is false.
  • The default value for type char is the null character. That means in char the value of (char) is ‘\u0000’.
  • The default value for all reference types (Sting or any Object parent or its child) is null.

If you have noticed, primitive data types are initialized in their form of number zero. The exception is boolean, where the default state is false. However, this representation of the default state comes from generation-lasting philosophical discussion beyond this blog’s scope.

Default data type for String and arrays

It is important to remember that in Java, an array is a container object that holds a fixed number of values of a single type.

The String object is unique in a certain way. It is because it is wrapping object around an array collection of primitive char data types. However, we always reference the String object, not String‘s internal array.

Array of primitive types keeps initialization value equal default value for each element of primitive data type.


double[] treePrimitiveDoubles = new double[3];

double zeroElementPrimitive = treePrimitiveDoubles[0];
System.out.print(zeroElementPrimitive);

>> 0.0

Array of reference type keeps initialization value equal null. The String is the same; it keeps the initialization value equal null.


Double[] treeReferenceDoubles = new Double[3];

Double zeroElementReference = treeReferenceDoubles[0];
System.out.print(zeroElementReference);

>> null

We instantly see that for initializing arrays of reference type, we need to create the objects to fill the array.

Filling default values to reference type arrays

There are two different approaches for reference type arrays on how to fill up an array by a particular type. First is the classic iterative approach, where you loop through the array, and you fill the position individually.


BankAccount[] arrayOfAllBankAccounts = new BankAccount[50];

for(int i = 0; i < arrayOfAllBankAccounts.length; i++) { arrayOfAllBankAccounts[i] = new BankAccount(); }

The second option is to use a more procedural approach with the help of java.util.Arrays. If you want to initialize a one-dimensional array with default values, you can use the static .fill() method. Although, internally .fill() method uses a loop. However, code notation is much more concise. Let's look at the code notation:


BankAccount[] arrayOfAllBankAccounts = new BankAccount[50];

Arrays.fill(arrayOfAllBankAccounts, new BankAccount());

Performance

Consider the array of arbitrary length which will store reference type for Object counterparts of primitive types such as Byte, Short, Integer, Long, Float, Double, Boolean and Char class. And imagine now that you would do this for many many arrays like this in your program. This would dramatically decrease performance of your application; you would waste a lot of machine cycles just for initialization. It would affect your application performance by considerable level.

Each array consisted of an Object version of primitive types that would not only take longer to initialize than a regular primitive type array, but it would even require allocating more memory than necessary. Consequently, if you want to use Object version of a primitive type, use them only when essential for your code solution.

Initialization of arrays with primitive types is quick and fast, does not allocate a lot of memory. You can prefer them over their reference type versions.

String performance

Java's String class has a special place in our hearts. As we said, String class uses an array of primitive characters for storing text. On each addition or change text, String needs to compute if the allocated array length is sufficient. If it is not, it needs to allocate a new - more significant size for the internal array of primitive characters and copy the existing text to a new array of primitive characters. Resizing mechanism naturally decrease performance. There are two ways how to mitigate this issue. First, it is highly recommended to not use concatenation on Strings in your code. And second, if you know you will be using a lot of text manipulation, use the StringBuilder class.

Conclusion

In this article, we have seen how to initialize the array in Java with zero values. As always, you can find all codes in this article in out GitHub repository.

This entry was posted in Language basics and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.