Initializing Arrays in Java

Arrays are widely used in many (almost any) programming languages. In Java, you have to specific the length of an Array at the creation of an Array.

Here is a short example how to initalize an Array containting the numbers 1, 2 and 3:

int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;

In order to abbreviate this, there is a short form which can do this work in one line. This works as Java can determine the length of the array by looking at the number of elements within the curly brackets. At compile time, the short form is translated to the form above within the byte code.

int[] numbers2 = {1,2,3};

Now, there are some quirks to be aware of regarding the short form. You can use this only while defining the array variable. The following will result in a compile error:

int[] numbers2 = {1,2,3};
numbers2 = {4,5,6}; // compile error!

Why does this happen? The error message says: “Array constants can only be used in initializers”. But I am not quite sure, what the meaning behind is. I consider it a quirk of the Java language without any further research regarding this construct. πŸ˜‰

However, today, I found a trick to circumvent this issue. You can find the solution in the Oracle Java Magazine, Section Fix This.

int[] numbers3 = {1,2,3};
numbers3 = new int[]{4,5,6};

I am not quite sure, why this works. Do you?


3 thoughts on “Initializing Arrays in Java

  1. Nice… haven’t seen this before. And as Google reveals this feature is called “anonymous array” in analogy to “anonymous classes” in Java – and can be used rather similar, e.g., to directly define an array as a parameter used in a method call:

    obj.findMax(new int[]{1,2,3});

  2. Thanks for your remarks, especially for the note on c++ initializer lists. One of the examples that the creators of Java tried to use the same syntax of c/c++ to make the language attractive to c/c++ developers. πŸ™‚

    As far as I see it, initializer lists in c++ are quite different to the java anonymous arrays.

    In Java, the “constructor” of an array only accepts a type and the length of the array. Using the initializer list syntax, the compiler determines the length of the list and passes it to the constructor as well as it will infer the type from the definition of the variable. Next, each element is stored in the array. In short, listing #2 of my post is translated to listing #1 by the compiler. This can be done manually using reflections (see ) as there is no Array class in the standard API.

    The idea of the trick is, that, as Matthias said, you can pass a newly created array to a method call. Otherwise, you _have_ to define an array variable as just calling

    obj.findMax({1,2,3}) ;

    will not compile. Normally, you would do the following (which takes an additional line):

    int[] args = {1,2,3};

    However, using this construct, you are able to do this in one line. And I would not consider it an ugly hack, as Java has quite a good garbage collector as well as many other programming languages which run on the JVM. πŸ™‚

    I would consider it bad naming style as the array does not have a name at all and, therefore, it is not clear what the intention, semantics and meanding of {1,2,3} is. This has to be made clear using a comment which could be substituted by naming the array using a variable. At execution time, the JIT compiler would optimize this so there is no difference at runtime.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s