5

The following code uses simple arrays of String in Java.

package javaarray;

final public class Main
{
    public void someMethod(String[] str)
    {
        System.out.println(str[0]+"\t"+str[1]);
    }
    public static void main(String[] args)
    {
        String[] str1 = new String[] {"day", "night"};
        String[] str2 = {"black", "white"};

        //Both of the above statements are valid.

        Main main=new Main();
        main.someMethod(str1);
        main.someMethod(str2);

        //We can invoke the method someMethod by supplying both of the above arrays alternatively.

        main.someMethod(new String[] { "day", "night" }); //This is also valid as obvious.
        main.someMethod({ "black", "white" }); //This is however wrong. The compiler complains "Illegal start of expression not a statement" Why?
    }
}

In the above code snippet, we can initialize arrays like this.

String[] str1 = new String[] {"day", "night"};
String[] str2 = {"black", "white"};

and we can directly pass it to a method without being assigned like this.

main.someMethod(new String[] { "day", "night" });

If it is so, then the following statement should also be valid.

main.someMethod({ "black", "white" });

but the compiler complains "Illegal start of expression not a statement" Why?

2
  • I guess you somehow need to pass the reference of the string array. By what you are doing, that does not happen. Commented Apr 12, 2012 at 3:47
  • 1
    It might be hard to parse properly in the form you want, for example if the compiler sees {1, 12, 57}, what should it be -- a byte[], short[], int[], or long[]? With the explicit type, the ambiguity is gone. Commented Apr 12, 2012 at 3:59

1 Answer 1

8

According to Java Language Specification (10.6. Array Initializers)

An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values:

So, there are only two ways you can use an array initializer ({"foo", "bar"}):

  1. Variable declaration: String[] foo = {"foo", "bar"};
  2. Array creation expression: new String[] {"foo", "bar"};

You can't use an array initializer as a method parameter.

15.10. Array Creation Expressions

ArrayCreationExpression:
    new PrimitiveType DimExprs Dimsopt
    new ClassOrInterfaceType DimExprs Dimsopt
    new PrimitiveType Dims ArrayInitializer 
    new ClassOrInterfaceType Dims ArrayInitializer
Sign up to request clarification or add additional context in comments.

4 Comments

You don't mean can't use it only ..., do you?
In this sentence So, you can use it only as a part of array creation expression can I know you are referring to it as?
@noMAD, it is an array initializer. I'll try to improve my answer, though.
Please include the JLS link so that it may also be more helpful to future visitors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.