4 Replies Latest reply: Sep 8, 2008 2:44 PM by 807589 RSS

    Casing Strings to string[] array

    807589
      Hi,
      Beginner question.

      Why can i do this:

      String[] tmp = {"mike", "Bob", "Jay"};
      SomeClass.main(tmp);

      But i can't do this:

      SomeClass.main({"mike", "Bob", "Jay"});

      It does not make sense to me.
      It would save me some time if i didn't have to declare a String[] all the time.

      Thanks,

      -J
        • 1. Re: Casing Strings to string[] array
          807589
          jmandawg77 wrote:
          String[] tmp = {"mike", "Bob", "Jay"};
          This is a syntax which only works for initialisers.
          SomeClass.main({"mike", "Bob", "Jay"});
          Because it does not know if you want an array of Object, Serializable, COmparable or String.

          You can do the foolwoing if you have used main(String... args)
          SomeClass.main("mike", "Bob", "Jay");
          or
          SomeClass.main(new String[] {"mike", "Bob", "Jay"});
          It would save me some time if i didn't have to declare a String[] all the time.
          Use varargs, this is what they are for.
          • 2. Re: Casing Strings to string[] array
            807589
            The Syntax {"a", "b", "c"} is an array initializer and it can't be used like an array literal. Here are two solutions:
            public class Example {
                public static void main(String[] args) {
                    f(new String[]{"a", "b", "c"});
                    g("a", "b", "c");
                }
            
                static void f(String[] v) {
                    for(int i=0; i<v.length; ++i) {
                        System.out.println(v);
            }
            }

            static void g(String... v) {
            for(int i=0; i<v.length; ++i) {
            System.out.println(v[i]);
            }
            }
            }
             [The String... syntax is a vararg|http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: Casing Strings to string[] array
              807589
              btw: you only call main() rarely if at all.
              • 4. Re: Casing Strings to string[] array
                807589
                Thanks guys, thats what i was looking for.
                I knew there was some way to do it, i just couldn't find it anywhere.

                -J