Java varargs method param list vs. array

前端 未结 6 1037
迷失自我
迷失自我 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:38

    The main reason not to specify everything as varargs is that it doesn't always make sense. For example, if InputStream.read(byte[]) where defined as `read(byte...) then the following call would be valid:

    myInputStream.read(0, 1, 2, 3);
    

    This would create a 4-element byte array, pass it in and then discard it.

提交回复
热议问题