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

前端 未结 6 938
情书的邮戳
情书的邮戳 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:31

    The difference is only when invoking the method. The second form must be invoked with an array, the first form can be invoked with an array (just like the second one, yes, this is valid according to Java standard) or with a list of strings (multiple strings separated by comma) or with no arguments at all (the second one always must have one, at least null must be passed).

    It is syntactically sugar. Actually the compiler turns

    function(s1, s2, s3);
    

    into

    function(new String[] { s1, s2, s3 });
    

    internally.

提交回复
热议问题