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

7

9

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});
    }
}
Threedecker answered 19/6, 2009 at 11:34 Comment(1)
As it happens, what became the varargs language features (as java.util.Arrays.asList) was originally going to do just that. It was probably a mistake to change it...Bevatron
G
19

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.

Gaudreau answered 19/6, 2009 at 11:38 Comment(0)
H
10

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.
Hernando answered 19/6, 2009 at 11:51 Comment(0)
T
4

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});
Tarrah answered 19/6, 2009 at 11:38 Comment(0)
C
4

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);
    }
}
Catherinacatherine answered 19/6, 2009 at 11:50 Comment(1)
Very nice ! I'll be using this.Threedecker
K
2

Java doesn't really have type inference. Array variable declarations are a special case in the Java language specification, one that does not apply to method parameters. Doing so would be possible, but would add a lot of complexity to the spec, since it would have to handle questions like whether {"a", "b"} creates a String[] or an Object[] - looks obvious here, but what if it's objects in a complex type hierarchy? And what if the method is overloaded and both versions exist?

Kibosh answered 19/6, 2009 at 11:46 Comment(0)
S
0

Curly braces (when used in array literals) may only be used when declaring the array :)

Siddon answered 19/6, 2009 at 11:56 Comment(0)
N
-1

Have you tried casting the list to int[] before passing it to the constructor?

Nazareth answered 19/6, 2009 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.