difference fn(String… args) vs fn(String[] args)

前端 未结 6 923
情书的邮戳
情书的邮戳 2020-11-28 03:47

Whats this syntax useful for :

    function(String... args)

Is this same as writing

    function(String[] args) 
<         


        
6条回答
  •  独厮守ぢ
    2020-11-28 04:36

    The only difference between the two is the way you call the function. With String var args you can omit the array creation.

    public static void main(String[] args) {
        callMe1(new String[] {"a", "b", "c"});
        callMe2("a", "b", "c");
        // You can also do this
        // callMe2(new String[] {"a", "b", "c"});
    }
    public static void callMe1(String[] args) {
        System.out.println(args.getClass() == String[].class);
        for (String s : args) {
            System.out.println(s);
        }
    }
    public static void callMe2(String... args) {
        System.out.println(args.getClass() == String[].class);
        for (String s : args) {
            System.out.println(s);
        }
    }
    

提交回复
热议问题