tags:

views:

134

answers:

6

Why is it that this works:

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

but this doesn't:

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

If I have an array instance variable and I want to initialize it in my constructor surely I don't have to go

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

I feel like I'm missing something here?

+4  A: 

The {} is syntatic sugar that allows you to fill your array with values upon initialization.

Justin Ethier
+4  A: 

You can also do this:

array = new int[]{1, 2, 3};

I'm not sure why it's necessary to specify the type on the righthand side in one version but not in the other.

Dan Dyer
Because it's a special shorthand language construct, Java needs a way to recognise you're using it.
Michael Clerx
+11  A: 

The literal syntax i.e. {} can only be used when initializing during declaration.

Elsewhere you can do the following instead:

int[] array;
array = new int[] {1, 2, 3};
missingfaktor
Thanks very much
avatarX
+2  A: 

Curly braces {} for initialization can only be used in array declaration statements. You can use:

int[] array = new int[] {1,2,3};  // legal

but not:

array = {1, 2, 3}; //illegal

http://www.janeg.ca/scjp/lang/arrays.html

Adam
A: 

array is a reference. This means that when you write array int[]; array will be equal to null. if you want to use it, you need first to allocate the array , by array = new int[3], and now you can fill it by.

array[0] = 1;
array[1] = 2;

Now, java has two syntax sugar for filling array. you can write arr int[] = {1,2,3} or arr int[] = new int[]{1,2,3}

Java will allocate and fill it behind the scene, but this is possible only on the deceleration.

roni
+1  A: 

The {...} construct here is called an array initializer in Java. It is a special shorthand that is only available in certain grammatical constructs:

JLS 10.6 Array Initializers

An array initializer may be specified in a declaration, or as part of an array creation expression, creating an array and providing some initial values. [...] An array initializer is written as a comma-separated list of expressions, enclosed by braces "{" and "}".

As specified, you can only use this shorthand either in the declaration or in an array creation expression.

int[] nums = { 1, 2, 3 };       // declaration

nums = new int[] { 4, 5, 6 };   // array creation

This is why the following does not compile:

// DOES NOT COMPILE!!!
nums = { 1, 2, 3 };

// neither declaration nor array creation,
// array initializer syntax not available

Also note that:

  • A trailing comma may appear; it will be ignored
  • You can nest array initializer if the element type you're initializing is itself an array

Here's an example:

    int[][] triangle = {
            { 1, },
            { 2, 3, },
            { 4, 5, 6, },
    };
    for (int[] row : triangle) {
        for (int num : row) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

The above prints:

1 
2 3 
4 5 6 

See also

polygenelubricants