Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a String array as argument

A String array can be declared and initialized in the following way:

String[] str = {"A", "B"};

but for a method which accepts a String array as argument, why can't the same be used there?

For example: if in the code below, i replace the call to show() from show(str); to show({"A" "B"});, it shows complier error. Why?

public class StringArray {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] str = {"A", "B"};
        show(str);
    }
    static void show(String[] s) {
        System.out.println(s[0] + s[1]);
    }
}

The compiler errors shown are:

StringArray.java:9: illegal start of expression
                show({"A", "B"});
                     ^
StringArray.java:9: ';' expected
                show({"A", "B"});
                      ^
StringArray.java:9: illegal start of expression
                show({"A", "B"});
                         ^
StringArray.java:9: ';' expected
                show({"A", "B"});
                          ^
StringArray.java:9: illegal start of type
                show({"A", "B"});
                               ^
StringArray.java:11: class, interface, or enum expected
        static void show(String[] s) {
               ^
StringArray.java:13: class, interface, or enum expected
        }
        ^
7 errors

Also using show(new String[] {"A", "B"}); is allowed. How is new String[]{"A", "B"} different from {"A", "B"} when passing them as method arguments? Thanx in advance!

like image 853
Surender Thakran Avatar asked Jul 18 '12 05:07

Surender Thakran


People also ask

Can you pass an array as an argument?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do you pass a String array to a parameter in Java?

Passing Array To The Method In Java To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);

Why array String is passed as argument in main method?

Because by passing String arrays , we can pass all the necessary parameters like options/arguments related to the program in the form of String easily. There can be several parameters! Also, all the other datatypes can be easily converted from String!


2 Answers

The syntax {"A", "B"} (without new String[] in front of it) can only be used as an array initializer expression. In all other contexts (including method calls), you need to use the new operator.

See the Java Tutorial on Arrays for more info.

like image 145
Ted Hopp Avatar answered Sep 30 '22 19:09

Ted Hopp


String[] str = {"A", "B"}; is minified version of String[] str = new String[]{"A", "B"};, Compiler doesn't know about plain {"A", "B"} as a string array unless you explicitly mention.

like image 30
RP- Avatar answered Sep 30 '22 19:09

RP-