functions with optional arguments java code example
Example 1: java how to make a parameter optional
void foo(String a, Integer b) {
}
void foo(String a) {
foo(a, 0);
}
foo("a", 2);
foo("a");
Example 2: java how to make a parameter optional
@SuppressWarnings("unchecked")
static <T> T getParm(Map<String, Object> map, String key, T defaultValue)
{
return (map.containsKey(key)) ? (T) map.get(key) : defaultValue;
}
void foo(Map<String, Object> parameters) {
String a = getParm(parameters, "a", "");
int b = getParm(parameters, "b", 0);
}
foo(Map.of("a","a", "b",2, "d","value"));