What is the difference between String[] and String… in Java?

前端 未结 5 719
面向向阳花
面向向阳花 2021-01-30 07:34

How should I declare main() method in Java?

Like this:

public static void main(String[] args)
{
    System.out.println(\"foo\");
}


        
相关标签:
5条回答
  • 2021-01-30 07:41

    Lets understand the difference via example and its Super easy

    mymethod(String... str)
       vs
    mymethod(String []str)
    


    Method getid take int type array.

    So if we call the getId method we need to pass array.
    To pass array we can create anonymous array or just simply first create an array and pass like i done this in below example.

    class test
    {
        public void getid(int []i)
        {
          for(int value:i)
         {
          System.out.println(value);
         }
        }
    
        public static void main(String []arg)
        {
         int []data = {1,2,3,4,5}
         new test().getid(data);
        }
    }
    

    Now below we Using triple dot -> mymethod(int... i)

    now the method still need the array but the difference is now we can pass direct value to that method and "..." automatically convert it to the array
    look example below

    class test
    {
        public void getid(int ...i)
        {
        for(int value:i)
        {
        System.out.println(value);
        }
        }
        public static void main(String []arg)
        {
        new test().getid(1,2,3,4);
        }
    
    }
    

    Advantage of using "..." instead of " [ ] "
    1) Its save memory :-
    in example one using mymethod(int [ ] )
    When we create an array in main method " Data[] "they create new object and get there space in memory
    as well as when we create method and define there argument like :-
    getid(int []) -> this will create another object in a memory so we have 2 object in a memory which is same to each other

    2) You can pass nothing while using "... "
    In example two you can pass nothing while calling "getid" method and they will work without giving any error which is very helpful to make program more stable for example

    .
    .
    .
        public static void main(String []arg)
        {
        new test().getid();
        }
    .
    .
    .
    


    But if we call "getid" method and didn't pass any argument while using "[ ] " then it will show the error at compile time

    0 讨论(0)
  • 2021-01-30 07:43

    String[] args takes an array argument.

    String... args takes an arbitrary number of strings as its argument and creates an array out of them.

    0 讨论(0)
  • 2021-01-30 07:44

    How should I declare main() method in Java?

    String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter (String...) you can call the method like:

    public void myMethod( String... foo ) {
        // do something
        // foo is an array (String[]) internally
        System.out.println( foo[0] );
    }
    
    myMethod( "a", "b", "c" );
    
    // OR
    myMethod( new String[]{ "a", "b", "c" } );
    
    // OR without passing any args
    myMethod();
    

    And when you declare the parameter as a String array you MUST call this way:

    public void myMethod( String[] foo ) {
        // do something
        System.out.println( foo[0] );
    }
    
    // compilation error!!!
    myMethod( "a", "b", "c" );
    
    // compilation error too!!!
    myMethod();
    
    // now, just this works
    myMethod( new String[]{ "a", "b", "c" } );
    

    What's actually the difference between String[] and String... if any?

    The convention is to use String[] as the main method parameter, but using String... works too, since when you use varargs you can call the method in the same way you call a method with an array as parameter and the parameter itself will be an array inside the method body.

    One important thing is that when you use a vararg, it needs to be the last parameter of the method and you can only have one vararg parameter.

    You can read more about varargs here: http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

    0 讨论(0)
  • 2021-01-30 07:46

    String[] is an array of Strings. Therefore a container of many variables of String data type. For example:

    String[] strArray = new String[2];
    str[0] = "Hello";
    str[1] = "World";
    
    String str = "Hello World";
    

    Hope this clears your queries :)

    0 讨论(0)
  • 2021-01-30 07:59

    String... gets converted to a String[]. The main difference is that you can call a vararg method in 2 ways:

    method(a, b, c);
    method(new String[] {a, b, c});
    

    whereas you need to call a method that accepts an array like this:

    method(new String[] {a, b, c});
    

    For the main method it does not make a difference.

    0 讨论(0)
提交回复
热议问题