methods - Does Java support default parameter values?

ID : 480

viewed : 148

Tags : javamethodsparametersoverloadingdefault-parametersjava





Top 5 Answer for methods - Does Java support default parameter values?

vote vote

92

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 when you have enough complexity that differentiating is difficult. A definite case is where you have to differentiate using the order of parameters, not just number and type.

vote vote

83

No, but you can use the Builder Pattern, as described in this Stack Overflow answer.

As described in the linked answer, the Builder Pattern lets you write code like

Student s1 = new StudentBuilder().name("Eli").buildStudent(); Student s2 = new StudentBuilder()                  .name("Spicoli")                  .age(16)                  .motto("Aloha, Mr Hand")                  .buildStudent(); 

in which some fields can have default values or otherwise be optional.

vote vote

73

There are several ways to simulate default parameters in Java:

  1. 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 two optional parameters of the same type and any of them can be omitted.

  2. Varargs.

    a) All optional parameters are of the same type:

    void foo(String a, Integer... b) {     Integer b1 = b.length > 0 ? b[0] : 0;     Integer b2 = b.length > 1 ? b[1] : 0;     //... }  foo("a"); foo("a", 1, 2); 

    b) Types of optional parameters may be different:

    void foo(String a, Object... b) {     Integer b1 = 0;     String b2 = "";     if (b.length > 0) {       if (!(b[0] instanceof Integer)) {            throw new IllegalArgumentException("...");       }       b1 = (Integer)b[0];     }     if (b.length > 1) {         if (!(b[1] instanceof String)) {              throw new IllegalArgumentException("...");         }         b2 = (String)b[1];         //...     }     //... }  foo("a"); foo("a", 1); foo("a", 1, "b2"); 

    The main drawback of this approach is that if optional parameters are of different types you lose static type checking. Furthermore, if each parameter has different meaning you need some way to distinguish them.

  3. Nulls. To address the limitations of the previous approaches you can allow null values and then analyse each parameter in a method body:

    void foo(String a, Integer b, Integer c) {     b = b != null ? b : 0;     c = c != null ? c : 0;     //... }  foo("a", null, 2); 

    Now all arguments values must be provided, but the default ones may be null.

  4. Optional class. This approach is similar to nulls, but uses Java 8 Optional class for parameters that have a default value:

    void foo(String a, Optional<Integer> bOpt) {     Integer b = bOpt.isPresent() ? bOpt.get() : 0;     //... }  foo("a", Optional.of(2)); foo("a", Optional.<Integer>absent()); 

    Optional makes a method contract explicit for a caller, however, one may find such signature too verbose.

  5. Builder pattern. The builder pattern is used for constructors and is implemented by introducing a separate Builder class:

     class Foo {      private final String a;       private final Integer b;       Foo(String a, Integer b) {        this.a = a;        this.b = b;      }       //...  }   class FooBuilder {    private String a = "";     private Integer b = 0;     FooBuilder setA(String a) {      this.a = a;      return this;    }     FooBuilder setB(Integer b) {      this.b = b;      return this;    }     Foo build() {      return new Foo(a, b);    }  }   Foo foo = new FooBuilder().setA("a").build(); 
  6. Maps. When the number of parameters is too large and for most of them default values are usually used, you can pass method arguments as a map of their names/values:

    void foo(Map<String, Object> parameters) {     String a = "";      Integer b = 0;     if (parameters.containsKey("a")) {          if (!(parameters.get("a") instanceof Integer)) {              throw new IllegalArgumentException("...");         }         a = (String)parameters.get("a");     } else if (parameters.containsKey("b")) {          //...      }     //... }  foo(ImmutableMap.<String, Object>of(     "a", "a",     "b", 2,      "d", "value"));  

Please note that you can combine any of these approaches to achieve a desirable result.

vote vote

61

Sadly, no.

vote vote

53

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.

Top 3 video Explaining methods - Does Java support default parameter values?







Related QUESTION?