Does Java support default parameter values?

后端 未结 25 2240
清歌不尽
清歌不尽 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:34

    I've now spent quite some time to figure out how to use this with methods that return values, and I haven't seen any examples so far, I thought it might be useful to add this here:

    int foo(int a) {
        // do something with a
        return a;
    }
    
    int foo() {
        return foo(0); // here, 0 is a default value for a
    }
    

提交回复
热议问题