Java optional parameters

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

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

相关标签:
17条回答
  • 2020-11-22 14:07

    There is optional parameters with Java 5.0. Just declare your function like this:

    public void doSomething(boolean... optionalFlag) {
        //default to "false"
        //boolean flag = (optionalFlag.length >= 1) ? optionalFlag[0] : false;
    }
    

    you could call with doSomething(); or doSomething(true); now.

    0 讨论(0)
  • 2020-11-22 14:07

    Java now supports optionals in 1.8, I'm stuck with programming on android so I'm using nulls until I can refactor the code to use optional types.

    Object canBeNull() {
        if (blah) {
            return new Object();
        } else {
            return null;
        }
    }
    
    Object optionalObject = canBeNull();
    if (optionalObject != null) {
        // new object returned
    } else {
        // no new object returned
    }
    
    0 讨论(0)
  • 2020-11-22 14:08

    There are no optional parameters in Java. What you can do is overloading the functions and then passing default values.

    void SomeMethod(int age, String name) {
        //
    }
    
    // Overload
    void SomeMethod(int age) {
        SomeMethod(age, "John Doe");
    }
    
    0 讨论(0)
  • 2020-11-22 14:09

    We can make optional parameter by Method overloading or Using DataType...

    |*| Method overloading :

    RetDataType NameFnc(int NamePsgVar)
    {
        // |* Code Todo *|
        return RetVar;
    }
    
    RetDataType NameFnc(String NamePsgVar)
    {
        // |* Code Todo *|
        return RetVar;
    }
    
    RetDataType NameFnc(int NamePsgVar1, String NamePsgVar2)
    {
        // |* Code Todo *|
        return RetVar;
    }
    

    Easiest way is

    |*| DataType... can be optional parameter

    RetDataType NameFnc(int NamePsgVar, String... stringOpnPsgVar)
    {
        if(stringOpnPsgVar.length == 0)  stringOpnPsgVar = DefaultValue; 
    
        // |* Code Todo *|
        return RetVar;
    }
    

    0 讨论(0)
  • 2020-11-22 14:10

    If it's an API endpoint, an elegant way is to use "Spring" annotations:

    @GetMapping("/api/foos")
    @ResponseBody
    public String getFoos(@RequestParam(required = false, defaultValue = "hello") String id) { 
        return innerFunc(id);
    }
    

    Notice in this case that the innerFunc will require the variable, and since it's not api endpoint, can't use this Spring annotation to make it optional. Reference: https://www.baeldung.com/spring-request-param

    0 讨论(0)
  • 2020-11-22 14:12

    varargs could do that (in a way). Other than that, all variables in the declaration of the method must be supplied. If you want a variable to be optional, you can overload the method using a signature which doesn't require the parameter.

    private boolean defaultOptionalFlagValue = true;
    
    public void doSomething(boolean optionalFlag) {
        ...
    }
    
    public void doSomething() {
        doSomething(defaultOptionalFlagValue);
    }
    
    0 讨论(0)
提交回复
热议问题