10 Replies Latest reply: Feb 28, 2008 3:52 AM by 807591 RSS

    Difference between (String....args) & (String [] args)

    807591
      Hi all

      I'm new to Java programming.
      Can anyone plz tell me what's the difference between (String....args) & (String [] args) If I'll use first one in place of the second........Does it make any difference?

      Thanks in advance
      Namrata
        • 1. Re: Difference between (String....args) & (String [] args)
          807591
          "String... args" will declare a method that expects a variable number of String arguments. The number of arguments can be anything at all: including zero.

          "String[] args" and the equivalent "String args[]" will declare a method that expects exactly one argument: an array of strings.

          One difference that might not spring out from those observations is that in the second case (but not the first) the caller may have a reference to the array. In both cases the method works with args as an array of Strings, but if it does things like swap the array elements this will not be visible to the caller if the "String... args" form is used.

          Edited by: pbrockway2 on Feb 28, 2008 9:33 PM
          Garbage removed.
          • 2. Re: Difference between (String....args) & (String [] args)
            807591
            Yes it does make a difference in calling part. as the first one the elipses will give you more flexibility in calling the method with any number of arguments but internally that too makes a array out of it. That is basically a representation. you can make the call to that method with any number of string parameters in case I and in case II you need to pass the String array defined within calling code.
            • 3. Re: Difference between (String....args) & (String [] args)
              807591
              Yes u r right...But I got this confusion b'coz of the following code...

              class Beta
              {
                        public void Foo(String...args)
                        {
                             System.out.println("Beta:Foo");
                        }
                        public void Bar(String a)
                        {
                             System.out.println("Beta:Bar");
                        }
              }

              class Alpha extends Beta
              {
              public void Foo(String a)
                        {
                             System.out.println("Alpha:Foo");
                        }
                        public void Bar(String a)
                        {
                             System.out.println("Alpha:Bar");
                        }
              }

              When I'll give the following lines by the main method:

              Beta a=new Alpha();

              a.Foo("test");      

              Then it should give Alpha:Foo

              But each time it is giving as Beta:Foo

              If i'll call using two parameteres like a.Foo("test","test1");
              then also it is giving ouput as Beta:Foo
              Why?????????Can u plz explain me???
              • 4. Re: Difference between (String....args) & (String [] args)
                807591
                The method works for 0.....n values. The thing happening is obvious as if you give a call to the method with single argument it is also the case of <method>(args1, args2, args3.....)
                So the JVM makes call to the method with elipses instead of single argument. Try to call the method without any argument it will not give compile time error because it is working for 0.....n arguments. For details read java specifications.

                Edited by: Sachin1904 on Feb 28, 2008 12:56 AM
                • 5. Re: Difference between (String....args) & (String [] args)
                  807591
                  Thanks. for ur reply....
                  That means in every case it will call Foo(String..args).
                  It wont call Foo(String a) in any case....
                  • 7. Re: Difference between (String....args) & (String [] args)
                    807591
                    Class Alpha has two Foo() methods: one taking a String argument, the other taking a variable number of arguments. They are overloaded methods.

                    Class Alpha has only a single Bar() method that overerides the Bar() method in Beta.

                    a is declared as a reference to a Beta. Beta has only a single Foo() method - the vararg one - which the compiler chooses. At runtime this gets invoked, not the overloaded alternative in class Alpha.

                    Things are quite different if you were to try a.Bar(). Again the compiler chooses the single Bar() method in the Beta. But now, at runtime, there is an overriding method in class Alpha that will actually get invoked.
                    • 8. Re: Difference between (String....args) & (String [] args)
                      807591
                      Just go through Runtime polymorphism specification. The reference in that case is Beta but the actual object is of alpha only.
                      • 9. Re: Difference between (String....args) & (String [] args)
                        YoungWinston
                        pbrockway2 wrote:
                        One difference that might not spring out from those observations is that in the second case (but not the first) the caller may have a reference to the array. In both cases the method works with args as an array of Strings, but if it does things like swap the array elements this will not be visible to the caller if the "String... args" form is used.
                        Is that true in all cases pbrockway2? What if I have the following setup:
                        public static void main(String[] args) {
                           for(String arg: args)
                              System.out.println(arg);
                           swapAround(args);
                           for(String arg: args)
                              System.out.println(arg);
                        }
                        private void swapAround(String... args) {
                           // do some swapping stuff
                        }
                        Are you saying that the whole args array is copied before handover to the swapAround() method? I checked the language specification, and I couldn't see any mention of it.

                        BTW: Thanks namratasingh. I wasn't aware of this new feature. Just shows how quickly you can get out of the loop.
                        • 10. Re: Difference between (String....args) & (String [] args)
                          807591
                          YoungWinston wrote:
                          pbrockway2 wrote:
                          One difference that might not spring out from those observations is that in the second case (but not the first) the caller may have a reference to the array. In both cases the method works with args as an array of Strings, but if it does things like swap the array elements this will not be visible to the caller if the "String... args" form is used.
                          Is that true in all cases pbrockway2?
                          Sorry - you are quite right and I was completely wrong when I posted that.

                          The "String... args" form just uses a reference to an array with no copying involved. If the caller already knows this reference, then they will keep on knowing it.

                          I don't know what (if anything) was going through my mind: except for the fact that it can happen that the argument is an expression that evaluates to an array.