Java optional parameters

后端 未结 17 1691
遇见更好的自我
遇见更好的自我 2020-11-22 13:19

How do I use optional parameters in Java? What specification supports optional parameters?

17条回答
  •  暖寄归人
    2020-11-22 13:57

    You can use something like this:

    public void addError(String path, String key, Object... params) { 
    }
    

    The params variable is optional. It is treated as a nullable array of Objects.

    Strangely, I couldn't find anything about this in the documentation, but it works!

    This is "new" in Java 1.5 and beyond (not supported in Java 1.4 or earlier).

    I see user bhoot mentioned this too below.

提交回复
热议问题