Skip to content

Tag: optional-parameters

How do I use optional parameters in Java?

What specification supports optional parameters? Answer There are several ways to simulate optional parameters in Java: Method overloading. void foo(String a, Integer b) { //… } void foo(String a) { foo(a, 0); // here, 0 is a default value for b } foo(“a”, 2); foo(“a”); One of the limitations of this approach is that it doesn’t work if you have