Java varargs method param list vs. array

前端 未结 6 1051
迷失自我
迷失自我 2020-12-01 01:50

Varargs:

public static void foo(String... string_array) { ... }

versus

Single array param:

public static void bar(         


        
6条回答
  •  忘掉有多难
    2020-12-01 02:35

    A vararg is simple syntactic sugar for an array.

    if you call foo("abc", "def", "ghi"); then compiler will call it as foo(new String[] {"abc", "def", "ghi"});

    compiler will create one new array and pass it to foo(). One can't have both foo(String...) and foo(String[]). Since both are functionally same.

提交回复
热议问题