java optional parameter in methods - java

I want to make a method that takes 1 required parameter and 1 optional parameter, but I found how to make an optional array which is by making in the parameter (int... b) but this is for an array, I want to make it just either this value is null or the user entered it, I can make it by making 2 methods of the same name but one with the single parameter and one with the 2 parameters, but can it be done with just one method?
Thanks

No, Java doesn't support optional parameters. Another alternative to overloading (which doesn't make much sense for two parameters but does make sense for more) is to use a builder type which represents all the parameters - you can provide a constructor for the builder which contains the required parameters, and then a setter for each of the optional ones, making the setter return the builder itself. So calling the method becomes something like:
foo.doSomething(new ParameterBuilder(10).setBar(2).setBaz(10));

What you are looking for is default arguments support. Java doesn't have this capability but it more or less simulates this capability.
One simple way is to use method overloading.
Another approach is to identify special cases directly and then substitute the default values.
Here's an example of mixing both of those approaches by Ivo Limmen:
public void test() {
this.test(null, null);
}
public void test(String p1) {
this.test(p1, null);
}
public void test(String p1, String p2) {
if(p1 == null) {
...
} else {
...
}
if(p2 == null) {
...
} else {
...
}
}
A very interesting approach I found is to use Design Builder pattern. There is an example here
There is also an interesting discussion here

In java, this is accomplished by something called method overloading. You can create multiple methods with the same name, but different parameters.
For example:
void method1(String arg1){
// only one argument, here you can delegate the work of this method to the implementation that takes the most parameters and pass null for the optional parameters.
method1(arg1, null);
}
void method1(String ar1, String arg2){
// do work, handle the case where arg2 is null.
}
Clients of your code can call either one. To them, it appears as if the second argument is optional.

No, this is exactly what method overloading is for

There are no "default" values for parameters in Java's methods. Either use varargs or method overloading.

Related

Is it a good practice to call to an overloaded method with null params?

this is a question about programming best practices, I didn't know how express the question in the Title, sorry, here we go.
I had a method in a Manager, or Controller, this way:
public boolean myMethod(Param1 param1);
And, because a change in the application, I had to redefine it like this, because it calls to other method which needs param2 and param3:
public boolean myMethod(Param1 param1, Param2 param2, Param3 param3);
Now I realize that the method with 3 params "always" (for now, maybe in the future there is a change and I need to call it with non-null params) will be called with param2=null and param3=null, so in the implementation of 1st method I did:
public boolean myMethod(Param1 param1) {
return this.myMethod(param1, null, null);
}
public boolean myMethod(Param1 param1, Param2 param2, Param3 param3) {
/* Call to other methods that is needed to pass it param2 and param3 */
}
So the call to the method of the Manager, and the original way, is,:
boolean isTrue = myManager.myMethod(param1);
This is one option, the other option is to pass the null params from the call:
boolean isTrue = myManager.myMethod(param1, null, null);
And let only one method in my Manager:
public boolean myMethod(Param1 param1, Param2 param2, Param3 param3);
So, the real questions are: What is the best way to do this talking about best practices? Is it wrong if in the implementation of tha Manager I overload a method and call it with null params?
Thanks in advance!
Greetings.
The overloaded method with fewer parameters that just call the other method with more parameters, is a common practice in Java, and is the Java way of implementing "default" parameters.
Note that the default value doesn't have to be null, it can be any value.
Normally when calling a method with multiple parameters, the values passed in will give a clue to the nature of the parameter. Parameter constants like null, false, true, or 0 doesn't give a clue to the parameters meaning, which makes the code is less readable.
Usually a call with fewer parameter is more obvious, so overloading with "default" parameters is preferable to only one method with a lot of constant parameters.
while we are at the topic:
Java 8 has introduced a new feature, specifically designed to support for multiple versions of an Interface: default methods. So let's say your Manager class is implementing MyInterface. So the first version of the Interface would be
public Interface MyInterface {
public boolean myMethod(Param1 param1);
}
then, with accordance to the ever changing world of technology, requirements change. so you need a new signature for myMethod(). with the default method feature, you could delegete the responsibility of backward compatilibity to the Interface:
public interface MyInterface {
// v1 with default implementation
default boolean myMethod(Param1 param1) {
return myMethod(param1, null, null);
}
// new v2 - pure virtual
public boolean myMethod(Param1 param1, Param2 param2, Param3 param3);
}
You can read more about Interface default methods in Oracle tutorial on the subject
This question depends on whether the code you are calling expects some of the parameters to be null. Given the popularity of this question I would recommend that you avoid sending null values as much as possible.
In this case, since you are the owner of the method you are passing null values to I would recommend that you have 2 versions of the method, one which can work with 1 parameter and another one which works with 3 parameters.
Changing existing methods is usually not recommended since it can break existing code. Adding new functionality on the other hand should leave previous work intact. It would also allow you to construct relatively pin pointed test cases since you would test the new behaviour not the entire previous behaviour and the new one.
I would propose to define the two methods you mentioned instead of providing only the 3-parameter method and forcing all users of the method to supply "null" as values. Providing the stripped-down 1-parameter method makes it explicit that the two other parameters are optional.
This is also recommended in the correct answer of this question.
It's not inherently good nor bad practice. As it was mentioned before, this is the approach of introducing default parameters in Java.
The only potential problem with having an overload that takes only one parameter and behind the scenes calls another overload (and passes other parameters by default) is when this is not clear from the method's documentation.
public boolean myMethod(Param1 param1) {
return this.myMethod(param1, null, null);
}
public boolean myMethod(Param1 param1, Param2 param2, Param3 param3) {
/* Call to other methods that is needed to pass it param2 and param3 */
}
You should document your first method properly in this case to make sure your users understand the implications of calling it. If you have an overload that takes parameters that you need to explicitly set to null, you don't have this "hidden" issue - however you should still document your method.
I think it's up to you really. Whatever you choose, make sure you document your API.
By the way, you may want to consider passing an object as parameter or employing some other patterns if the list of parameters grows and your are forced to have a huge number of overloads.

In Java is using the ellipsis to represent an optional parameter a good idea?

I'm wondering if using the ellipsis to represent an optional parameter is a good idea?
public void update(Object object, boolean... checkFirst)
{
if (checkFirst[0])
{
...
} //if
} //update
I'm using code that connects to a DB table that doesn't have a primary key to allow multiple inserts (legacy code so please excuse any bad practice thoughts here). So instead of using two methods (overloading) I was thinking I could possibly use the ellipsis to achieve the same effect. But do you think this would be terrible practice?
Cheers,
Alexei Blue.
The proper term for the ellipsis is varargs. Varargs allow a developer to pass a variable number of arguments.
It is not an good idea to use varargs to provide one optional argument. It is better to use overloading as in the code example below.
public void update(Object object)
{
update(update, false);
}
public void update(Object object, boolean check)
{
if (check)
{
...
} //if
}
This way a developer is unable to pass in multiple booleans but is allowed to pass in none.

Overloading method calls with parameter null [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Method Overloading for NULL parameter
In the code below the output is
String
and if I remove the method with the parameter of type String then the output is
Object
I know how overloading of methods acts when the parameter types don't match exactly but I can not understand how null can be treated as an Object and/or a String parameter.
What is the explanation for this?
class C {
static void m1(Object x) {
System.out.print("Object");
}
static void m1(String x) {
System.out.print("String");
}
public static void main(String[] args) {
m1(null);
}
}
It always uses the most specific method according to the Java specs, section 15.12.2.5.
The intro is reasonably specific about it:
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
Generally speaking, and at least for code readability, it's always best to try to be as explicit as possible. You could cast your null into the type that matches the signature you want to call. But that's definitely a questionable practice. It assumes everyone knows this rule and makes the code more difficult to read.
But it's a good academic question, so I +1 your question.
When multiple overloads match a signature, Java picks the most specific method from among them.
The value of null matches both Object and String, but String is a subclass of Object, so String is picked. If you add another overload with a sibling of String in the class hierarchy, you'd get a compile error.\
// DOES NOT COMPILE
class C {
static void m1(Object x) {
System.out.print("Object");
}
static void m1(String x) {
System.out.print("String");
}
static void m1(Integer x) {
System.out.print("Integer");
}
public static void main(String[] args) {
m1(null);
}
}
Here is a link to a post that discusses your code example at some length.
If you need to force the call of a aprticular overloaded method when passing null as parameter, you have to cast it, like this:
m1((String)null);
By doing this, you make sure you're calling the correct overloaded version of the method.
You have to set the type of null to tell Java what function you want to call.
So you do: m1((Object) null); to call the implementation with the Object parameter and you do m1((String) null); to call the other one.
1. As String is also an object the JVM got confused to call which method at runtime.
2. If you want to specify which method to call at runtime, you can do this by explicit casting
eg:
m1((String)null);

Create an object with a String and method overloading

I have a String which can either be of Double or Integer type or some other type. I first need to create a Double or Integer object and then send it over to a overloaded method. Here's my code so far;
public void doStuff1(object obj, String dataType){
if ("Double".equalsIgnoreCase(dataType)) {
doStuff2(Double.valueOf(obj.toString()));
} else if ("Integer".equalsIgnoreCase(dataType)) {
doStuff2(Integer.valueOf(obj.toString()));
}
}
public void doStuff2(double d1){
//do some double related stuff here
}
public void doStuff2(int d1){
//do some int related stuff here
}
I'd like to do this without if/else, with something like this;
Class<?> theClass = Class.forName(dataType);
The problem is 'theClass' still can't be cast to either double or int. I would be gratefull for any ideas.
Thanks.
Found a related thread; Overloading in Java and multiple dispatch
This is not just a problem of dealing with primitive types.
Which method to call is decided in compile time, that is, if you want to be able to call different methods depending on the type of the arguments, you'll need several calls (i.e. you need the if-construct).
In other words, it wouldn't work even if doStuff2 took Integer and Double as arguments (your code is basically as good as it gets).
(In fancy words, this is due to the fact that Java has single dispatch. To emulate multiple dispatch you either need to use conditional statements or a visitor pattern.)
Since the method call is decided at compile time as the another answer told you, overloading won't work for you. I think that this problem can be solved with inheritance. So you write a base class with yourMethod() and override it in your derived classes.
As aioobe says, the choice between overloaded methods is made at compile time based on the static types of the arguments.
If you want to simulate overload choice at runtime, you will need to do some complicated runtime analysis of the different possible methods. It would go something like this:
get all declared methods of the class that declared doStuff2.
filter out the methods whose name is not doStuff2.
filter out the methods whose argument type cannot be assigned from the (dynamic) type of the argument value.
of the remaining methods, pick the one that is the best match ... taking care to deal with "ties" as ambiguous.
This will be tricky to code, and trickier if you also throw in handling of primitive types. It will also make the method calls expensive.
Frankly, some kind of hard-wired dispatching is much simpler. If you don't like if / else tests (or switching on a String in Java 7), then you could do something like this.
Map<String, Operation> map = ...
map.put("Double", new Operation(){
public void doIt(Object obj) {
doStuff2((Double) obj);
}});
map.put("Integer", new Operation(){
public void doIt(Object obj) {
doStuff2((Integer) obj);
}});
...
map.get(typeName).doIt(obj);
... which at least allows you to "plug in" support for new types dynamically.
If you resort to reflection, you'll only have to deal specially with primitive types. So your technique can work, but with the addition of a few explicit tests. If you need to reflectively find a method that accepts a primitive double, use double.class.

Java varargs: a way to get back to the ...-form?

Guess you have a class with some c-tors:
public Foo(int i, String s);
public Foo(float fl, String s);
public Foo(String s, Bar b, boolean f);
Now, when you have the following fn:
public Foo doSomething(Object… args)
{
/*… do something before … */
Foo foo = new Foo( ?!? );
/*… do something after … */
return foo;
}
What should one to to call the correct c-tor? Is there a way to translate args back into the ...-form?
No - after all, there could be any references in there, including null references.
You'd have to check the values you've actually been given against the parameter types for the constructor signatures.
I'd regard this as a sign that you probably shouldn't be using varargs in this situation. You could always overload the method with the same signatures as the constructors, and call common helper methods for the before/after parts... or if the time of the constructor call doesn't matter, do it all in a common method, except the construction:
public void doSomething(int i, String s)
{
doSomethingHelper(new Foo(i, s));
}
Apart from inspecting the concrete type of each element in args, casting them down manually, then invoking the proper constructor via reflection, there isn't much you can do. (And that would be ugly like hell...) The compiler needs to bind constructor calls statically, but you only get to know the concrete parameters at runtime.
An alternative would be to provide a constructor with varargs parameter, but this just delegates the problem one level lower. #Jon's suggestion of getting rid of the varargs altogether is better.
You can, of course check the number and type of the arguments and dispatch accordingly. But note my comment above.
Note that Object... is really just syntactic sugar for Object[]. So you have the same options here as you would if your parameter type was Object[].

Categories