Detailed Notes
Welcome. Today we are taking an introductory look at the Java Optional Class. We will focus primarily on the Optional Class and what it is for. Then we will cover why it is useful and how to use it in our code. Finally, we wrap up with four hands examples of using the different types of Optional methods in our code.
What is Java the Optional Class?
The optional class is essentially a container object, which may or may not contain a non-nullable value.
Why should we use the Java Optional Class?
One of the biggest problems with Java is that when we create variables or methods with return types, our intent is unclear if the value of these will be null.
String name; String getName();
void doSomething(){ assertThat("My Name").isEqualTo(getName()); } He is a simple example where we have a string name, a method that returns a string getName(), and then some method that tries to compare a string name equal to our getName(). The problem with this is name could be null and throw a NullPointerException at runtime. However, just reading the code, we don't know, for instance, if the name was ever instantiated or populated so it could contain a null value.
The code is very unclear on the intent of the name. Should it always be populated, or could the name be null? How, as developers, do we know the name could be null? We don't because the name is just specified as a name.
Unfortunately, this approach leaves it up to the developer to determine the intent of the variable if it can be null. This leads the developer to implement many null checks within their code.
if (name != null) { then do something... } Primarily what do we do if the name is not known? What do we want to do with it? If we don't know that name could be null, then when we try to use it, we will get a runtime exception all over the place when we try to use the name. This is where Optional comes in.
Why Is Java Optional useful?
When we use the optional class, we imply our intent that a variable or our method could potentially return a no result or a null. Because the Optional class gives us the ability to wrap our variables and methods. Telling the developer that this variable is optional and that name might not have a value in a particular instance; therefore, the variable will not return a value.
Additionally, the Optional class has some methods that handle how we do null pointers or null checks. Same with methods, like our Optional String getName. So this time, in the above code, we create a new Optional String for both the variable and method getName.
By doing this, we are specifying that name may return no result so that it could be null, but because we're using Optional, we will know that we need to handle that case.
Transcript Text
foreign [Music] [Music] for example two we are going to create an optional object from an object so we're going to actually create an object first and then convert it to an option we're going to look at some of the caveats or things we need to be careful of when we use the of method and then we're going to create an optional with an object that could be null so in example two we want to create an optional object from an object so we need to start with an object so for that I will create a string so let's start with a string and let's call it uh onion and we'll just name it onion or Simplicity all right so now we have a string variable onion with a string value of onion now to convert this to an optional again we start with optional we take the generic type for string and we're going to call this one our optional string Now to create or I should say to convert this string object to an optional object we use the of method of the optional class so use optional Dot of and of returns an optional describing the given non-null value so now if we do onion we are happy so optional string so optional string our string could be it null of onion so we're going to create our optional from our string object here onion all right let's make sure that we have a value so now we can do assert that optional string Dot is present Dot equals to I'm sorry is equals to now that we have our optional string of optional we're going to assert that optional securing is present which should return true I like using the assert that's because this is very readable all right so we create an object optional object from an object string onion we convert it to an optional object optional type and then we verify that that option string was stored as an option so if we save this run as and we pass so in this case optional string is present now what happens if we do this slightly differently so what if we did a string no string and we have a string value of null well of is not going to like this of will actually throw a runtime exception so to test that to verify that we will do assert so we'll assert that exception of type so we're expecting a null pointer exception is thrown by do a function call here all right so we'll take optional so we're going to do the same thing here optional of and we're going to take our null string so we're asserting that we should get an exception of no pointer exception when we try to set our null string to an optional so if we run this that is the case so how do we fix this how do we avoid this well with the introduction of I think Java 9 maybe Java 10. they changed this so they added an option that allows us to create a nullable option so for that we start with a new optional string foreign so this will be an option with null string and we can set it equal to optional dot of nullable and this will return an object describing the given value if not null otherwise it will return an empty optional so this handles the null pointer Exception by converting it to an empty optional so we do all nullable and then here we can pass in our null string so now we should not get a null pointer exception instead our option with null string should return an empty nullable or empty uh optional so let's check that so let's assert that our option with no string Dot is empty Dot is equal to true so what we're doing here is we're going to check to see if the option optional we created here with the nullable string is empty and that it returns true so we're checking to make sure that this guy is null or is it it returns empty when null instead of throwing the null pointer exception so if we run this it passes all right so for example two we started out by creating an optional object from an object we started with the string onion with some text onion we then created an optional variable and instantiated our optional string with our string onion using optional of we looked at being cautious of using optional of with nulls so if you pass in a null object and try to convert it to nullable with of you will get a null pointer exception however optional did add an additional method to handle nulls called of nullable so if you declare a string or declare an object as nullable or an object up to option and it is Noble you need to use of nullable [Music] thank you
Transcript Segments
foreign
[Music]
[Music]
for example two we are going to create
an optional object from an object
so we're going to actually create an
object first and then convert it to an
option
we're going to look at some of the
caveats or things we need to be careful
of when we use the of method and then
we're going to create an optional with
an object that could be null
so in example two we want to create an
optional object from an object so we
need to start with an object
so for that I will
create a string so let's start with a
string
and let's call it uh onion
and we'll just name it onion or
Simplicity all right so now we have a
string variable onion with a string
value of onion
now to convert this to an optional again
we start with optional
we take the generic type for string
and we're going to call this one our
optional
string
Now to create or I should say to convert
this string object to an optional object
we use the of method of the optional
class so use optional Dot
of and of returns an optional describing
the given non-null value
so now if we do onion
we are happy so optional
string so optional string our string
could be it null of onion so we're going
to create our optional
from our string object here onion
all right let's make sure that we have a
value so now we can do assert
that
optional string
Dot
is present
Dot
equals
to I'm sorry is equals to
now that we have our optional string of
optional we're going to assert that
optional securing is present which
should return true
I like using the assert that's because
this is very readable all right so we
create an object optional object from an
object string onion
we convert it to an optional object
optional type
and then we verify that that option
string was stored as an option so if we
save this
run as
and we pass so in this case optional
string is present
now
what happens if we do this slightly
differently so what if we did a string
no string
and we have a string value of null well
of is not going to like this of will
actually throw a runtime exception so to
test that to verify that we will do
assert
so we'll assert that exception
of type so we're expecting a null
pointer exception
is thrown by
do a function call here all right so
we'll take optional
so we're going to do the same thing here
optional of
and we're going to take our null string
so we're asserting that we should get an
exception of no pointer exception when
we try to set our null string to an
optional
so if we run this
that is the case
so how do we fix this how do we avoid
this
well
with the introduction of
I think Java 9 maybe Java 10. they
changed this so they added an option
that allows us to create a nullable
option so for that we start with a new
optional string
foreign
so this will be an option
with null
string
and we can set it equal to
optional
dot of nullable and this will return an
object describing the given value if not
null
otherwise it will return an empty
optional so this handles the null
pointer Exception by converting it to an
empty optional so we do all nullable and
then here we can pass in our null string
so now we should not get a null pointer
exception instead our option with null
string should return an empty nullable
or empty uh optional so let's check that
so let's assert
that
our option
with no string
Dot is empty
Dot is equal to
true
so what we're doing here is we're going
to check to see if the option optional
we created here with the nullable string
is empty and that it returns true so
we're checking to make sure that this
guy
is null or is it it returns empty when
null instead of throwing the null
pointer exception so if we run this
it passes
all right so for example two we started
out by creating an optional object from
an object we started with the string
onion
with some text onion we then created an
optional variable and instantiated our
optional string with our string onion
using optional of
we looked at being cautious of using
optional of with nulls so if you pass in
a null object and try to convert it to
nullable with of you will get a null
pointer exception
however optional did add an additional
method to handle nulls called of
nullable so if you declare a string or
declare an object as nullable or an
object up to option and it is Noble you
need to use of nullable
[Music]
thank you