Does Java support default parameter values?

I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all three parameters here } I know that in C++ I can assign a parameter a default value. For example: void MyParameterizedFunction(String param1, int param2, … Read more

Set a default parameter value for a JavaScript function

I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn’t defined (and ignored if the value is passed). In Ruby you can do it like this: def read_file(file, delete_after = false) # code end Does this work in JavaScript? function read_file(file, delete_after … Read more

Does Java support default parameter values?

No, the structure you found is how Java handles it (that is, with overloading instead of default parameters). For constructors, See Effective Java: Programming Language Guide’s Item 1 tip (Consider static factory methods instead of constructors) if the overloading is getting complicated. For other methods, renaming some cases or using a parameter object can help. This is … Read more

“Least Astonishment” and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5]. The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() … Read more