Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Technical reason for no default parameters in Java

Tags:

I've been looking around to try to find what the reasoning is behind not including default parameters for functions in Java.

I'm aware that it's possible to simulate the behavior, either with varargs or else by creating several overloaded functions that accept fewer parameters, and call the real function that takes all parameters. However, neither of these options match the clarity and ease-of-use of, e.g. C++'s syntax.

Does anyone know if there's a solid technical reason that would make something like

void myFunc(int a=1, int b=2) {...}

undesirable or undo-able in a new version of Java?

like image 352
Kricket Avatar asked Nov 26 '10 09:11

Kricket


People also ask

Can you have default parameters in Java?

Short answer: No. Fortunately, you can simulate them. Many programming languages like C++ or modern JavaScript have a simple option to call a function without providing values for its arguments.

Which function Cannot have default parameters?

Constructors cannot have default parameters.

Does Java support optional parameters?

Java allows to pass null as an argument to set default values for optional parameters! Well, specifying null to indicate that a given argument is optional might seem like the easiest option ever! You can also use @Nullable annotation, it makes it clear and simple that a method can accept null arguments!

What is non default parameter?

The Python "SyntaxError: non-default argument follows default argument" occurs when we define a function with a positional parameter that follows a default parameter. To solve the error, make sure to specify all default parameters after the positional parameters of the function.


2 Answers

It was not in the initial version of Java because they decided they did not need it, probably to keep things simple.

Adding it now would be tricky, because it needs to be done in a backwards-compatible fashion. Adding varargs, autoboxing and generics in Java5 was a major undertaking, and it could only be done with limited functionality (such as type erasure) and at the cost of increased complexity (the new method resolution rules make for good exam trick questions).

Your best shot would be with a non-Java language on the JVM. Maybe one of them already has this.

like image 119
Thilo Avatar answered Mar 20 '23 00:03

Thilo


I am not aware of a technical reason, apart from it being complicated which values are being omitted and which ones are not.

For example, in your sample, if only one integer was passed through then is it a or b that should be defaulted? Most probably a but it does add that level of ambiguity.

A simple solution would be to

void myFunc(Integer a, Integer b) {
  if (a == null) a = 1;
  if (b == null) b = 2;

}

Yes it is more long winded, and yes it hides the defaulting within the code, rather than the method signature (which could then be shown in JavaDoc), but it does enforce the consistency.

like image 21
mytest Avatar answered Mar 20 '23 02:03

mytest