Hi everyone.
Welcome back to this tutorial.
In this tutorial, we'll code and explore the of nullable of an empty method that is part of the optional
class.
The first step is I'm going to create a class called Optional of empty.
Nullable example.
Let's make this class executable by adding public static void main.
The first method that we are going to explore is a off label method, which is part of the optional
object.
And the method that I'm going to create is going to return optional of type string.
So I'm going to keep this example really simple.
That's the reason why I'm going to explore the different methods using the optional of type string.
Public.
Static optional of string.
This is what it is going to return.
And we are going to explore the of nullable.
How do we create the.
Optional.
How do we make this method return the optional of type string?
So I'm going to use the optional class dot.
So you get all these methods, right?
We are going to explore off nullable and then it accepts an input.
So since we are going to return optional of type string, I'm going to pass.
Optional of hollow the return type of this method.
If you go to this method, the return type is going to be optional of type that you are passing.
The type that we are passing is string.
So this is going to return optional of string.
Let's give it a name string optional and then return it.
Button string optional.
So let's go ahead and run this.
Doubt.
And then.
Condition.
Check the result.
The result is optional and the hollow string is wrapped inside this optional.
How do we get access to that value?
The way we get access to that value is optional.
Dot get.
That's that way we can actually get the ring that it encapsulates.
The next question is under what scenarios we would use optional of nullable.
Let's go to this method and check this method.
Check this first condition.
If the value equal to equal to null, then it is going to return an empty optional.
Otherwise it is going to give you the off value.
So if you are not certain this value is going to be a valid string all the time, then you would use
optional dot of nullable.
What will happen if I pass null here?
So this one is going to give us.
An empty, optional object.
How do we check whether a value is present in an optional object or not?
We have seen that in the previous tutorial is present is a method which will tell you whether that optional
has a valid value inside.
That optional object or not.
So this is all about optional.
Let's go ahead and explore the next method.
We are going to explore about the of method, which is part of the optional.
Instead of the off nullable we are going to use off.
Here.
I'm going to give hello as an.
Input.
And then we're going to call the off method.
Of.
Let's run this example and check.
Let me give some string here so that it will be able to differentiate between this and the other one.
Is going to be off.
Run this.
If you run this.
Here we are doing the.
Is present.
I'm going to just remove this.
I'm going to make this.
Accept a valid string.
I'm going to run this again and then let's check the result.
If both are having the valid string, then if you take a look at it, both are returning you the same
result.
What would happen if I pass the value as null here?
Let's go ahead and run this and check the result.
We passed the value as null.
It gives you an exception called null pointer exception because this method always expects you to send
a valid input as a valid value, as an input to this method.
That is not the case here.
If you pass null, what will happen?
It is going to return you the empty optional object.
So if you are not, if you are certain all the time, you're going to have a valid string.
In those kind of scenarios, we would use the off method.
Otherwise you would use the off nullable method.
I'm going to return it back to its original state and run this and check the result.
There you go.
The next condition.
The next method that we are going to explore is.
The empty method, which is part of the optional class itself.
Empty.
I'm going to just return.
Optional dot.
Empty.
Then will be a written statement.
Right?
Optional dot empty.
So this one is going to give you the empty object as a result.
Empty.
But condition check the result.
Now this will give you an empty optional object.
So under what scenarios you would use empty.
Let's say you have a method that returns this.
Right?
And those that returns an type of optional In those kind of scenarios, you can have empty in the exception
block.
So in that case, you add her to the actual method return type to always return optional as an output
of that method.
So this is all about of nullable of an empty method.
Let's explore some more use cases and some more methods that are part of the optional in the next tutorial.
With this we came to the end of this tutorial.
Thank you for watching.