Hi everyone.
Welcome back to this tutorial.
In this tutorial, I'll give you all an introduction about optional.
Optional is introduced as part of Java eight and this is used to represent a non null value in Java.
The main purpose of this one is to provide a way to avoid the most common exception, which is the null
pointer exception and avoid having too many null checks in your code.
Any experienced developer would have come across a null pointer exception at some point in their life.
The concept of optional is inspired from the other new languages such as Scala, Groovy and Eccetera.
Let's go ahead and take a look at an example.
Apparently English open.
What I'm going to do, I'm going to create a package called optional.
Package all optional.
And then I'm going to name a class called Optional Example.
Okay, that's good.
Then I'm going to make this class executable by adding public static void main method.
The use case that we are going to code is that I'm going to create a method that is going to return
me a string, which is going to be the name of the student, and then I'm going to print the length
of that name.
So I'm going to create a method here.
The method name is going to be public static, and this is going to return the name of the student and
the return type is going to be string, and the name of the method is going to be get student name.
So I'm going to use the student database class.
Yard.
There is a student supplier which just supplies a student.
Take a look at it.
We are receiving a student called Adam as part of this method.
But this supplier is going to supply the student object and I'm going to call the get method.
So let's consider this one as like a database call.
You're making a database call here.
But actually, behind the scenes, it is just a method which is running the student.
But let's assume this is going to make the database call and then returns the student.
Right.
And let's give it a name.
So if the student is not found, this is going to return null.
So what I'm going to do, I'm going to have the null check here.
If the student is not equal to null, then return.
Student dot getName.
Okay.
Else.
I'm going to return null.
Otherwise, instead of else, I'm going to put the return statement below.
Return null.
Okay, so let's call this method and print the length of the student name.
That's the actual use case, right?
Student Not the student.
It's going to be string name equal to get student name.
Sounds good.
And then what I'm going to do, then I'm going to print the length of the student.
Length of the student name.
In here, what we are going to do, we are going to do name dot length.
This is going to print the length of the student name.
I'm going to run this example.
There you go.
It printed the length as four.
If you go to the student supplier method, this is returning the student name as Adam, and the length
of the name is equal to four.
So what is going to happen is the student is not available in the database, right?
So in that case, we are going to pass null.
And then I'm going to run this program.
Let's see what's going to happen.
So this is going to give me the null pointer exception because I don't have the null check here.
And what I'm going to do, I'm going to add the name not equal to null.
Basically, I'm adding a null check.
So if the name is not equal to null, get the length of the student.
Else you can give name not found.
Name not found.
So now if you take a look at the student, we are having multiple null checks in many places.
Let's say you want to get into the student activities.
Then again, we have to check the length of that student array list and then print the student activities.
So if this object is having many properties and if you want to access each and every property, then
you might have to perform the null check of each and every property and it is going to bloat your code
with all these null checks.
Now we will handle the same scenario using optional and then we'll compare.
What is the difference between these two methods?
These two approaches and what are the advantages of one over the other?
We want to create a method in here.
It's going to be get student name optional.
And in here, what I'm going to do, I'm going to call the optional class dot.
There are many different ways of creating an optional object, but in here I'm going to use the off
nullable method.
We will be covering all these different methods that are part of the option in the future tutorial.
But for now I'm going to use the optional nullable and then I'm going to call this.
Basically what we are doing here is that we are wrapping the actual student object inside the optional
right?
So it's going to be optional of student and then let's give it a name.
Student Optional.
So if you take a look at the code, this code is basically taking this student object actually, and
then wraps it with the optional object.
That's what is happening.
Right.
So how do we check whether this option is having a value or not?
How do we check in this case, whether this object has a optional has a student inside that optional
object or not?
There is a handy method which is available, which is called student optional Dot is present.
This is to check whether is this optional object has any value inside that object or not.
If it is there, what we are going to do, we are going to return the student name.
Right?
That's a use case.
We are coding here.
It is going to return.
Student optional dot.
Map.
There is a handy method called map.
If you take a look at it, there are many methods which are available.
I'll be covering all these methods in the future tutorials, but for now we are going to use a map method.
What do you want to map?
We want to return the name.
Right.
Let's use the method reference syntax and then do the get name.
So this is going to return the.
So the name.
But again, this is going to return the student name inside this optional so optional of type string.
So here the return type is going to be optional of string.
Instead of returning the plain old string, we are going to return the optional of string, which is
going to actually hold the value.
So in here we return the null right in here.
What we are going to do, we are going to return optional of empty.
Basically option of empty represents
an optional object.
But no value.
That's what it represents.
Okay, now I'm going to comment this code.
Let's run this one.
So it's going to return optional of string and let's give it a name.
It's going to be string optional equal to get student name optional.
So that's the method which we are going to call.
So like how we perform the not null check we are going to perform string optional dot is present.
If it is present, then get the name.
So how do we get the actual value?
The way to get it is by using string optional dot get.
So in here we use a map method to get to the actual name of it.
If you want to just get the student object itself, then you can call the get method.
This will return the.
Didn't object.
Then Here, this will return the string, which is student name.
Okay.
And then what do you want to do?
We want to calculate the length of the student.
Right.
Let's copy this code here and then put it here.
So here we are, printing the length of the student name.
Let's run this code and then check the result.
If you take a look at it, the length of the student name is four.
If it is not present.
What we want, what we want to do in the case of else, we will provide.
Name not found.
Right.
Let's run this and check.
You run it, it's going to give you the length of the student of the name.
Length of the student name, actually.
And then what I'm going to do here, let's change this.
Let's change this code to return the option of nullable.
What are the values?
What this is going to do.
This is going to return optional dot empty.
So when this code runs, when this is present, check is run, basically this is going to return false
and then we are going to return the actual object.
So instead of returning this, we can do this.
Also, we can return the student optional or if we need the optional dot string, right?
So this represents optional of student.
So in here, when we pass null what it is going to return, it is going to return optional dot empty
when this check runs for the empty optional object this is going to.
Evaluate to false and then it is going to return optional empty.
And in here, what is going to happen?
This is going to this is going to evaluate to false and then it is going to print name not found.
Let's run this and check if we take a look at it.
That's the result we got.
So you might be wondering what's the difference here?
Right.
Let me move away from the presentation mode so that I can show you the whole code.
So in here, if you take a look at it, this method is returning null and this method is returning option
dot empty.
So we replaced not null.
Check with the is present check.
Right.
That might be running in your mind.
What is the difference here?
The difference between the null and optional here is that you can still execute a method call on an
empty optional object.
So in here, even though this get student name optional returns empty, you will still be able to check
whether that value inside that optional object is present or not on that empty optional object and the
return type of this method here, if you take a look at it, the return type is here we are returning
null null doesn't belong to any type.
Anything can be null, a string can be null, the student object can be null.
But in the case of this method, it is always going to return the optional type optional as a support
for a lot of additional methods which can be used for variety of use cases.
We will be exploring all those methods in the following tutorials, but for now this is all about optional.
Let's talk about more different scenarios in the future.
Tutorials.
With this we came to the end of this tutorial.
Thank you for watching.