10

I have a method having an array parameter like:

public static void foo(int[] param) {
    // Some code
}

And also I can call the method by writing like

foo(new int[3]);

Normally, we declare and initialize an array by new operator or double braces initializer like {1, 2, 3}. For example, int[] foo = new int[3]; or int[] foo = {1, 2, 3};.

But it's impossible to use double brace initializer as a parameter for a method. {} is only available for creating an array object.

And here is my question: Are there any differences between new operator and {}? If there is, what is it?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Ren lee
  • 103
  • 5
  • thanks for your comment, Scary. and that's exactly my point. foo ({1, 2, 3}) is not working... – Ren lee Mar 11 '15 at 07:45
  • 4
    `foo (new int[] {1, 2, 3})` works – Eran Mar 11 '15 at 07:45
  • @Eran yes, but the question here is why does int[] i = {1,2,3}; work but not foo({1,2,3}); – Subler Mar 11 '15 at 07:48
  • Does int[] foo = {1, 2, 3} really work? I had no idea. Just when you thought you knew all the syntax of the java language.. ;) – Chetan Kinger Mar 11 '15 at 07:49
  • may be... double braces VS `new` is int x[] = new int[10] you are allocating 10 locations (only length, no values) , they contain init-value based on type (primetive types) or NULL for objects, however, `{1,4,7}` will init values + length or `{}` will mean new array with `length=0` – Yazan Mar 11 '15 at 07:49
  • maybe because {1,2,3} is not an array but just the initialization values for an array? – Andrei Nikolaenko Mar 11 '15 at 07:50
  • thanks Eran. certainly foo (new int[] {1, 2, 3}) works but foo ({1, 2, 3}). my point is there is some way to create an array like new int[3], new int[] {1,2,3}, and {1,2,3} using new operator or {}. but when we call a method, we can only use new operator. not {}. i guess there is some kind of differences between new operator and {}, and want to know that. – Ren lee Mar 11 '15 at 07:52
  • 14
    Note: What you show here is **not** what is known as [double brace initialization](http://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java). Use the correct terminology to avoid confusion. – Jesper Mar 11 '15 at 07:56
  • Check out the "Creating, Initializing, and Accessing an Array" section of this [link](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – dbank Mar 11 '15 at 07:57
  • Foo({3,4,5}) really doesnt have a datatype defined. That would work in python but in java you require a datatype – Zuko Mar 11 '15 at 09:46
  • By the way, you're using the term double braces initialization wrong. This is what you would call it: new ArrayList(){{add(1);}} Related question: http://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java – Kirill Rakhman Mar 11 '15 at 10:58
  • 1
    You might want to use var-args instead of an array parameter: `void foo(int...params)` and call with `foo(1, 2, 3)`. – Walter Laan Mar 11 '15 at 11:18

2 Answers2

17

The {} as part of a int foo[] = {1, 2, 3}; is what is termed as brace initialization, and is a shortcut for int foo[] = new int[]{1, 2, 3}; because the new int[] can be inferred from the left hand side.

In the second case foo({1, 2, 3}); no inference of what you intend can be determined as there is no hint as to what the {1, 2, 3} means, so the compiler complains.

When you rewrite it as foo(new int[]{1, 2, 3}); you're telling the compiler what the item in {} is meant to represent.

The compiler won't (and does not try) to auto-interpret the {} expression until it matches - that would lead to potential ambiguities.

There is also this question, which seems to cover exactly the same ground.

As mentioned by @benzonico, it is part of the language specification as to this being a supported syntax, which doesn't include it being used in the invocation of a method.

Community
  • 1
  • 1
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
5

Just to complete the other answer, you can have a look at the JLS 8 10.6: you can see that in the grammar a variable initializer is either an expression OR (exclusive) an array initializer (which is not an expression and thus cannot be use when invoking a method).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
benzonico
  • 10,635
  • 5
  • 42
  • 50