Passing a Literal Array to a Method

Discussion in 'Java' started by KevinSimonson, Mar 3, 2011.

  1. Java lets one assign a literal array to a variable, and then pass the
    variable
    to a method. Why can't one just pass the literal array to the
    method? For
    example, I can write:

    public class Bug
    {
    private static void processArray ( int number
    , String[] strngArray)
    {
    System.out.println( "Number is " + number + '.');
    for (int index = 0; index < strngArray.length; index++)
    { System.out.println
    ( "Array position " + index + " is \"" + strngArray[ index] +
    "\".");
    }
    }

    public static void main ( String[] arguments)
    {
    String[] sa = { "Abc", "Def" };
    processArray( 7, sa);
    }
    }

    and have my code work just fine, but if I try:

    public class Bug
    {
    private static void processArray ( int number
    , String[] strngArray)
    {
    System.out.println( "Number is " + number + '.');
    for (int index = 0; index < strngArray.length; index++)
    { System.out.println
    ( "Array position " + index + " is \"" + strngArray[ index] +
    "\".");
    }
    }

    public static void main ( String[] arguments)
    {
    processArray( 7, { "Abc", "Def" });
    }
    }

    and then compile it I get error messages:


    sh-4.1$ javac Bug.java
    Bug.java:15: illegal start of expression

    processArray( 7, { "Abc", "Def" });

    ^

    Bug.java:15: ';' expected

    processArray( 7, { "Abc", "Def" });

    ^

    Bug.java:15: illegal start of expression

    processArray( 7, { "Abc", "Def" });

    ^

    Bug.java:15: ';' expected

    processArray( 7, { "Abc", "Def" });

    ^

    Bug.java:15: illegal start of type

    processArray( 7, { "Abc", "Def" });

    ^

    Bug.java:17: class, interface, or enum expected

    }

    ^

    6 errors

    sh-4.1$

    Anybody know why this happens? Is there any way to pass a literal
    array to a
    method?

    Kevin Simonson
     
    KevinSimonson, Mar 3, 2011
    #1
    1. Advertising

  2. On Mar 3, 12:14 pm, Leif Roar Moldskred <> wrote:
    >
    > Java need to know the type of the array before it can dispatch on it.
    > Is { "Abc", "Def" } an String[], CharSequence[] or Object[]? The answer
    > isn't obvious.
    >
    > You can do what you want in Java, however, but the syntax is a little
    > more involved:
    >
    >   processArray( 3, new String[] { "Abc", "Def" } );
    >
    > --
    > Leif Roar Moldskred


    Thanks Leif! This worked just fine!

    Kevin Simonson
     
    KevinSimonson, Mar 3, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. deadpickle
    Replies:
    1
    Views:
    999
    Jens Thoms Toerring
    Nov 7, 2010
  2. Stephen Miller
    Replies:
    12
    Views:
    778
    Stephen Miller
    Jan 20, 2004
  3. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    383
    Anonieko Ramos
    Sep 27, 2004
  4. avasilev
    Replies:
    9
    Views:
    393
    Goran
    Jan 5, 2012
  5. dorno

    Passing literal with reference?

    dorno, Jun 11, 2007, in forum: Perl Misc
    Replies:
    16
    Views:
    163
    Uri Guttman
    Jun 19, 2007
Loading...

Share This Page