Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why passing {a, b, c} to a method doesn't work?

I've tried to pass an initialization list {...} to a constructor and it didn't work. When I instead declared it in a method local variable (int[]) it worked flawlessly.

Why is that?

public class QuickSort {
    int[] a;

    public QuickSort(int[] a) {
        this.a = a;
    }

    public static void main(String[] args) {
        // ###################
        // ###    WORKS     ##
        // ###################
        int[] a = {8,12,79,12,50,44,8,0,7,289,1};
        QuickSort sort = new QuickSort(a);

        // ###################
        // ### DOESN'T WORK ##
        // ###################
        //QuickSort sort = new QuickSort({8,12,79,12,50,44,8,0,7,289,1});
    }
}
like image 986
Maxim Veksler Avatar asked Jun 19 '09 11:06

Maxim Veksler


4 Answers

When declaring an int[] and assigning {1, 2, 3} the compiler knows you want to create an int[] as it's spelled out right there.

In the latter case where you stick the array directly into the method call you would have to use

QuickSort sort = new QuickSort(new int[] {8,12,79,12,50,44,8,0,7,289,1});

to tell the compiler what your array is.

like image 142
Joey Avatar answered Nov 13 '22 03:11

Joey


The {} construct is called an array initializer, and it is used to initialize an array in Java. (Reference: Section 10.6: Array Initializers from The Java Language Specification, Third Edition.)

The reason why passing {1, 2, 3} itself is not valid is because there is no type information associated with the initializer.

Therefore, one must let the compiler know the type of the array is by writing new Type[], where the Type is the type for which the array is made for.

The following are all valid use of the array initializer:

  • new String[] {"Hello, "World"}
  • new Character[] {'A', 'B'}
  • new Runnable[] {new Runnable() {public void run() {}}, new Runnable() {public void run() {}}

As can be seen, this notation can be used for many data types, so it's not something that is specific for integers.

As for:

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

The reason why the above is valid is because the type information is provided to the compiler in the variable type declaration, which in this case is int[]. What the above is implying is the following:

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

Now, if we have new int[] {1, 2, 3}, we are able to create a new int[] array in place, so that can be handled as any other int[] array would -- it's just that it doesn't have a variable name associated with it.

Therefore, the array created by new int[] {1, 2, 3} can be sent into an method or constructor that takes a int[] as its argument:

new Quicksort(new int[] {1, 2, 3});  // This will work.
like image 28
coobird Avatar answered Nov 13 '22 05:11

coobird


This is probably because your initialization list has no typing information to it. Try this:

QuickSort sort = new QuickSort(new int[] {8,12,79,12,50,44,8,0,7,289,1});
like image 4
user125661 Avatar answered Nov 13 '22 05:11

user125661


You also can do like this:

public class QuickSort {
    int[] a;

    public QuickSort(int ... a) {
        this.a = a;
    }

    public static void main(String[] args) {

        QuickSort sort = new QuickSort(8,12,79,12,50,44,8,0,7,289,1);
    }
}
like image 4
Artem Barger Avatar answered Nov 13 '22 05:11

Artem Barger