Whenever we design a method in a Java class, a parameter can be optional for the execution of that method. First, we specify the required parameters and then we define the optional parameters.
In this article, we will learn about the optional parameters in java and implementation using different methods with syntax and example:
Java Optional Parameters
In some scenarios, while defining the method, we don't know the exact number of parameters that the user can provide to the function. In such cases, we opt for java optional parameters which take parameters as optional.
Optional Parameters mean even if a user is not providing the arguments, the function will get executed without any error. In the following sections, we will discuss multiple ways to implement optional parameters in java.
Before that let's look at the below if else code:
In the above example, we use an if-else block to check whether the parameter passed by the user is null or not, if it is not null then we will implement that method else we will do some default operations.
In this case, we have only 1 parameter to encounter but think about the code in which we have numerous methods each with many parameters that can be optional or not. Can we use it else for all of them?
Obviously, the answer is NO! So, java optional parameters also avoid this code smell.
04 Methods to Implement Java Optional Parameters
Let's now look at 4 ways on how do you add optional parameters in Java.
01) Overloading Method
In the Overloading method, method, we will define the same method again and again but each one with a different number of optional parameters.
First, we define the method with the required parameters and then we redefine the same method but with 1 optional parameter and then redefine it with 2 optional parameters, and so on...
When working with optional parameters, method overloading is one of the more obvious and common approaches available.
The basic syntax of the method overloading to have Java optional parameters is given below:
An example of this method overloading is shown below:
Note that in the above program we are able to provide the two, three, and four arguments to the same method with any error.
02) Optional container object
In Java, optional is a container object that can return a non-null value. If a value is not null, then the isPresent() method will return true, and as a result, the get() method will return its value. In case we are not sure about its value (can be null or not null), we use the ofNullable() method of this class.
It returns an empty Optional object and does not throw an exception. Now, let's look at the basic syntax of creating an optional container object below.
Given below is the syntax of how we can create an optional container object in java:
Here, we provided the second parameter as an optional parameter.
Let's take a look at the below example in which we first imported the Optional class as we will be using it in our program and then implemented an optional container object to have java optional parameters.
Note that in the above program we have not provided the last name in the second call still we are able to run the program without any error with the output of the Last name not given!
03) Built Pattern to have Java optional parameters
This is a very interesting way to design patterns to create complex objects. The main purpose is to separate the construction of an object from its representation! The builder pattern is yet another approach to handling Java optional parameters! Now let's see how it works.
Given below is a practical example of creating patterns to have optional java parameters. First, we have a public static nested class inside our Main class. StudentBuilder is our builder class. Then we will define all the fields of the outer class of the StudentBuilder.
After that, we will create a public constructor for the StudentBuilder with all the required properties as parameters and then we will also create methods for optional parameters. An example is given below:
04) Varargs (variable length arguments)
Varargs means variable-length arguments which allow the method to accept 0 or multiple parameters. As this method creates maintenance problems therefore this method is usually not suggested.
It could be handled in two ways. One uses an overloaded method(one for each) and another is to put the arguments into an array and then pass this array to the method. Both methods are error-prone and require further more code. Let's first see its basic syntax of it.
Three dots are used to declare varargs in Java. The syntax of which is shown below :
Here we are implicitly declaring an array a of type int and telling the compiler that FunctionName() can be called with 0 or more arguments.
Let's take a look at the example below:
Note that, first we haven't provided any arguments, as a result, we get 0 and then we provided three arguments, as a result, we get 3 in the output as shown above.
In some scenarios, while creating user-defined functions we don't know the exact number of arguments that the user will provide to our function and this is where we need the java optional parameters. From this article, we may now know how to declare and implement optional parameters in java with various methods.
Congratulations on getting this far! Now give yourself a pat on the back. Good job!