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] so let's start with example one so we are going to create an empty optional object we are going to start by looking at using the empty method then we're going to after we create our object we're then going to check to see if the optional is empty and then we're going to check to see if the optional is present all right so let me jump out of the slides all right so our first example we're going to start by creating an empty optional object now I've went ahead and created these four example classes as a unit tests so we're going to use the assert J the assert that class from the assert Date Presentation to kind of quickly show some comparisons and validate what we're doing so the first one here we're going to create an object that create an empty optional object he had some spaces here so we can read this all right so the first thing we need to do is we need to create an optional object so you start with the optional so we start with the optional class which if we note is in the Java util optional and it takes some type of generic so we have to tell it what type of optional we want to create so for simplicity's sake let's just use our string class and let's just call this empty optional string and to create an empty optional you use the optional class again and you don't need to type it and we can use the empty method which returns an empty optional instance no value is present for option so if we use empty so no value is present for this option all right so this is how we create an optional object and if we read it we understand the intent so we have an optional string so this string value empty optional string potentially could be empty in our particular case because we called optional.empty we know it is empty so I named it empty optional string so again our naming Convention of our methods and our variables help to imply the intent of what the code is doing all right so second let's now do an assertion to check to see if that optional is empty now to check if the optional is empty we again use the empty optional string that we just created but in this case we're going to use the is empty method and because this is an optional empty implementation here this should return true so to do a simple assert test we can do assert that so we could do this two ways one we can use the assert that and we can check that it's empty or we can do it this way we can take a certain empty is empty however this will simply return true or false but if we want to show intent which a certain J does we want to say okay we want to assert this is something so we follow this with DOT is equal to true so now with the search J we read this as we want to assert that in our empty optional string is empty is true so if we run this we pass so good so empty optional string is empty returns true because we have an empty optional declaration here all right so now how can we check to see that the optional is not present so we can check that it's empty but we can also check to see if it's not present so if it's present that means that it's not empty so if we do is present we'll return a value if present it will return true otherwise false so if we do is present this should be false and we pass all right so for example one we want to create an empty optional object to do that we start with optional we then set the generic for the object type that we want so in this case string we named it empty optional string and then we instantiated with the optional dot empty method which just returns an empty optional instance no value is present then we check to see if the optional is empty so we took our variable we called the is empty method which will return true because our optional is empty and then lastly for example one we want to check to see if the optional is not present so we again used our optional variable and we used the method is present to verify that the optional is not there that it is empty so if a value is present we would get true otherwise we got false which is what we expected [Music] thank you
Transcript Segments
foreign
[Music]
[Music]
so let's start with example one so we
are going to create an empty optional
object we are going to start by looking
at using the empty method then we're
going to after we create our object
we're then going to check to see if the
optional is empty and then we're going
to check to see if the optional is
present
all right so let me jump out of the
slides
all right so our first example
we're going to start by creating an
empty optional object now I've went
ahead and created these four example
classes as a unit tests so we're going
to use the assert J the assert that
class from the assert Date Presentation
to kind of quickly show some comparisons
and validate what we're doing
so the first one here we're going to
create an object that create an empty
optional object
he had some spaces here so we can read
this
all right so the first thing we need to
do is we need to create an optional
object so you start with the optional
so we start with the optional class
which if we note is in the Java util
optional
and it takes some type of generic so we
have to tell it what type of optional we
want to create so for simplicity's sake
let's just use our string class
and let's just call this empty
optional
string
and to create an empty optional you use
the optional class again
and you don't need to type it and we can
use the empty method which returns an
empty optional instance no value is
present for option
so if we use empty
so no value is present for this option
all right so this is how we create an
optional object and if we read it we
understand the intent so we have an
optional string so this string value
empty optional string
potentially could be empty
in our particular case because we called
optional.empty we know it is empty so I
named it empty optional string
so again our naming Convention of our
methods and our variables help to imply
the intent of what the code is doing
all right so second let's now do an
assertion to check to see if that
optional is empty now to check if the
optional is empty we again use the empty
optional string that we just created but
in this case we're going to use the is
empty method
and because this is an optional empty
implementation here this should return
true
so to do a simple assert test we can do
assert that
so we could do this two ways one we can
use the assert that
and we can check that it's empty or we
can do it this way we can take a certain
empty
is empty
however this will simply return true or
false but if we want to show intent
which a certain J does we want to say
okay we want to assert this is something
so we follow this with DOT is
equal to
true
so now with the search J we read this as
we want to assert that in our empty
optional string is empty is true
so if we run this
we pass
so good
so empty optional string is empty
returns true because we have an empty
optional declaration here
all right so now how can we check to see
that the optional is not present so we
can check that it's empty but we can
also check to see if it's not present
so if it's present
that means that it's not empty
so if we do is present
we'll return a value if present it will
return true otherwise false so if we do
is present
this should be false
and we pass
all right so for example one we want to
create an empty optional object to do
that we start with optional
we then set the generic for the object
type that we want so in this case string
we named it empty optional string and
then we instantiated with the optional
dot empty method which just returns an
empty optional instance no value is
present
then we check to see if the optional is
empty so we took our variable we called
the is empty method which will return
true because our optional is empty
and then lastly for example one we want
to check to see if the optional is not
present so we again used our optional
variable and we used the method is
present to verify that the optional is
not there that it is empty so if a value
is present we would get true otherwise
we got false which is what we expected
[Music]
thank you