Does Java support default parameter values?

后端 未结 25 1623
清歌不尽
清歌不尽 2020-11-22 07:07

I came across some Java code that had the following structure:

public MyParameterizedFunction(String param1, int param2)
{
    this(param1, param2, false);
}         


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

    No, but you can very easily emulate them. What in C++ was:

    public: void myFunction(int a, int b=5, string c="test") { ... }
    

    In Java, it will be an overloaded function:

    public void myFunction(int a, int b, string c) { ... }
    
    public void myFunction(int a, int b) {
        myFunction(a, b, "test");
    }
    
    public void myFunction(int a) {
        myFunction(a, 5);
    }
    

    Earlier was mentioned, that default parameters caused ambiguous cases in function overloading. That is simply not true, we can see in the case of the C++: yes, maybe it can create ambiguous cases, but these problem can be easily handled. It simply wasn't developed in Java, probably because the creators wanted a much simpler language as C++ was - if they had right, is another question. But most of us don't think he uses Java because of its simplicity.

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

    No, but the simplest way to implement this is:

    public myParameterizedFunction(String param1, int param2, Boolean param3) {
    
        param3 = param3 == null ? false : param3;
    }
    
    public myParameterizedFunction(String param1, int param2) {
    
        this(param1, param2, false);
    }
    

    or instead of the ternary operator, you can use if:

    public myParameterizedFunction(String param1, int param2, Boolean param3) {
    
        if (param3 == null) {
            param3 = false;
        }
    }
    
    public myParameterizedFunction(String param1, int param2) {
    
        this(param1, param2, false);
    }
    
    0 讨论(0)
  • 2020-11-22 07:24

    Unfortunately, yes.

    void MyParameterizedFunction(String param1, int param2, bool param3=false) {}
    

    could be written in Java 1.5 as:

    void MyParameterizedFunction(String param1, int param2, Boolean... params) {
        assert params.length <= 1;
        bool param3 = params.length > 0 ? params[0].booleanValue() : false;
    }
    

    But whether or not you should depend on how you feel about the compiler generating a

    new Boolean[]{}
    

    for each call.

    For multiple defaultable parameters:

    void MyParameterizedFunction(String param1, int param2, bool param3=false, int param4=42) {}
    

    could be written in Java 1.5 as:

    void MyParameterizedFunction(String param1, int param2, Object... p) {
        int l = p.length;
        assert l <= 2;
        assert l < 1 || Boolean.class.isInstance(p[0]);
        assert l < 2 || Integer.class.isInstance(p[1]);
        bool param3 = l > 0 && p[0] != null ? ((Boolean)p[0]).booleanValue() : false;
        int param4 = l > 1 && p[1] != null ? ((Integer)p[1]).intValue() : 42;
    }
    

    This matches C++ syntax, which only allows defaulted parameters at the end of the parameter list.

    Beyond syntax, there is a difference where this has run time type checking for passed defaultable parameters and C++ type checks them during compile.

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

    Instead of using:

    void parameterizedMethod(String param1, int param2) {
        this(param1, param2, false);
    }
    
    void parameterizedMethod(String param1, int param2, boolean param3) {
        //use all three parameters here
    }
    

    You could utilize java's Optional functionality by having a single method:

    void parameterizedMethod(String param1, int param2, @Nullable Boolean param3) {
        param3 = Optional.ofNullable(param3).orElse(false);
        //use all three parameters here
    }
    

    The main difference is that you have to use wrapper classes instead of primitive Java types to allow null input.Boolean instead of boolean, Integer instead of int and so on.

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

    No. In general Java doesn't have much (any) syntactic sugar, since they tried to make a simple language.

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

    As Scala was mentioned, Kotlin is also worth mentioning. In Kotlin function parameters can have default values as well and they can even refer to other parameters:

    fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size) {
        ...
    }
    

    Like Scala, Kotlin runs on the JVM and can be easily integrated into existing Java projects.

    0 讨论(0)
提交回复
热议问题