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] [Music] foreign [Music] optional class so this is just going to be an introduction to the optional class in Java we're primarily going to be doing an overview of the optional class so we're going to look at what is it why is it useful and how do we use so let's start with what it is so the optional class is essentially a container object which may or may not contain a non-nullable value so why is this useful well one of the biggest problems with Java is even from the the day it was created is the intent of our methods the intent of our variables is not really clear if we're going to have a value or not so we have an example here string name stirring get name and then some method that actually tries to compare a string name equal to our get name which should return the name the problem with this is name could be null and throw a null pointer exception at runtime so just reading the code we don't know for instance if name potentially could contain a null value so it's very unclear on the intent so how do we know the name could be null we don't because name is just specified as name unfortunately this approach leads up to the developer to determine the intent of the variable if it can be null so we then have to add a lot of null checks within our application so is name equal to null his name not no and what do we want to do with it if we don't then we're going to get runtime exceptions all over the place and this is where optional comes in so why is optional useful so the optional class now gives us the ability to wrap our strings or wrap our variables and wrap our methods with a optional class which basically implies the intent that our variable or our method could potentially return a null result or a null but optional in and of itself it handles the notes so optional basically says okay name is optional meaning name might not have a value in this particular case names not going to return null because the optional class has some methods that handle How We Do null pointers or null checks same with our methods here our optional string so we create a string a return type forget name so again we're specifying that name May return no result so it could potentially be blank but because we're doing optional we're going to know that we need to handle that no or we need to at least filter out that in all value so this is going to be a very Hands-On driven presentation today so we've got four examples with the first example being we're going to create an empty optional object so we're going to look at the basics of optional how to create the objects how to check if the optional is empty and how to check if the optional has a value our second example we're going to go a little bit deeper into the optional methods we're going to look at how to create an optional object from an object so we can take that string and actually convert it to an optional we're also going to look at some of the issues of using the of method which can't be null this will throw an old pointer exception so we'll look at some caveats and things to watch out for of the basics of using some of these simple methods and then the third piece we're going to look at in the second example is we're going to look at creating an optional with an object that could be known so we're going to look at converting an object to an optional we're going to look at some of the issues of of how we could get a null pointer exception and then we're going to look at creating an optional with nullable so we're actually going to show how to create an optional showing the intent that it could contain nullable values for our third example we are going to dive deeper into the optional class and actually start looking at some of the conditional options that we have with the optional class so we're going to look at how to get our object out of the optional so we're going to look to see how we can get that string value out of our optional string we're going to look at comparisons between null checks versus the optional if present method we're also going to look at the if present or else which was introduced in Java 9. and then we're going to look at some of the other conditional methods as well like or else or else get or else throws and with Java 10 they introduced a simplified version of the or else which just throws no such element exception and for our last example example four we're going to look at using the streamed methods to map our optional objects into their object counterparts so this could be useful for jpas for entities for or just plain old objects we can also using the filter method to actually remove nulls or pull out the specific data that we want from within our option or within our streams very useful and we're also going to look at using multiple filters to pull out specific values so you can almost look at filter like a where clause in a SQL statement very cool with streams and then lastly we'll look at the flat map method which basically allows you to take a flattened list of streams or flattened list of objects and map them out in a flat map option any questions or comments is Java option used so Java options is so as you saw where I was using the streaming there in the last example when streams were introduced in Java 8 they were introduced through optional and early on it wasn't quickly adopted until Java 9 and 10 came out when they introduced the additional supplement methods to help reduce the overhead but what it allows for is you can now chain or do method chaining instead of having to do all the nasty if null checks to verify if a particular method or variable potentially can be null it also implies what the actual intent is of the variable or the method basically if optional is there that tells you that you need to be conscious that there could potentially be a null value the value may or may not be instantiated okay got it with that I'd like to thank everyone for your time if you'd like to discuss any of these topics further or if you have any additional topics you would like us to cover please reach out to us we're available through email at info developmenter.com you can send us a message on our website developmenter.com contact us you can find us on YouTube search for YouTube developer or use the developer tag we are also available on Vimeo vimeo.com developer Facebook also facebook.com developer Twitter same twitter.com developreneur our goal is making every developer better have a great day [Music] [Music] [Music] foreign [Music]
Transcript Segments
foreign
[Music]
[Music]
[Music]
foreign
[Music]
optional class so this is just going to
be an introduction to the optional class
in Java
we're primarily going to be doing an
overview of the optional class so we're
going to look at what is it why is it
useful and how do we use
so let's start with what it is so the
optional class is essentially a
container object which may or may not
contain a non-nullable value
so why is this useful well one of the
biggest problems with Java is even from
the the day it was created is the intent
of our methods the intent of our
variables is not really clear if we're
going to have a value or not so we have
an example here string name
stirring get name and then some method
that actually tries to compare a string
name equal to our get name which should
return the name
the problem with this is name could be
null and throw a null pointer exception
at runtime
so just reading the code we don't know
for instance if name potentially could
contain a null value so it's very
unclear on the intent
so how do we know the name could be null
we don't because name is just specified
as name
unfortunately this approach leads up to
the developer to determine the intent of
the variable if it can be null so we
then have to add a lot of null checks
within our application
so is name equal to null his name not no
and what do we want to do with it
if we don't then we're going to get
runtime exceptions all over the place
and this is where optional comes in
so why is optional useful so the
optional class now gives us the ability
to wrap our strings or wrap our
variables and wrap our methods with a
optional class which basically implies
the intent that our variable or our
method could potentially return a null
result or a null but optional in and of
itself it handles the notes so optional
basically says okay name is optional
meaning name might not have a value
in this particular case
names not going to return null because
the optional class has some methods that
handle How We Do null pointers or null
checks
same with our methods here our optional
string
so we create a string a return type
forget name
so again we're specifying that name May
return no result so it could potentially
be blank but because we're doing
optional we're going to know that we
need to handle that no or we need to at
least filter out that in all value
so this is going to be a very Hands-On
driven presentation today
so we've got four examples with the
first example being we're going to
create an empty optional object so we're
going to look at the basics of optional
how to create the objects how to check
if the optional is empty and how to
check if the optional has a value
our second example we're going to go a
little bit deeper into the optional
methods we're going to look at how to
create an optional object from an object
so we can take that string and actually
convert it to an optional we're also
going to look at some of the issues of
using the of method which can't be null
this will throw an old pointer exception
so we'll look at some caveats and things
to watch out for of the basics of using
some of these simple methods
and then the third piece we're going to
look at in the second example is we're
going to look at creating an optional
with an object that could be known so
we're going to look at converting an
object to an optional we're going to
look at some of the issues of of how we
could get a null pointer exception and
then we're going to look at creating an
optional with nullable
so we're actually going to show how to
create an optional showing the intent
that it could contain nullable values
for our third example we are going to
dive deeper into the optional class and
actually start looking at some of the
conditional options that we have with
the optional class
so we're going to look at how to get our
object out of the optional so we're
going to look to see how we can get that
string value out of our optional string
we're going to look at comparisons
between null checks versus the optional
if present method
we're also going to look at the if
present or else which was introduced in
Java 9.
and then we're going to look at some of
the other conditional methods as well
like or else or else get or else throws
and with Java 10 they introduced a
simplified version of the or else which
just throws no such element exception
and for our last example example four
we're going to look at using the
streamed methods to map our optional
objects into their object counterparts
so this could be useful for jpas for
entities for or just plain old objects
we can also using the filter method to
actually remove nulls or pull out the
specific data that we want from within
our option or within our streams
very useful and we're also going to look
at using multiple filters to pull out
specific values
so you can almost look at filter like a
where clause in a SQL statement very
cool with streams
and then lastly we'll look at the flat
map method
which basically allows you to take a
flattened list of streams or flattened
list of objects and map them out in a
flat map option
any questions or comments
is Java option used
so Java options is so as you saw where I
was using the streaming there in the
last example
when streams were introduced in Java 8
they were introduced through optional
and early on it wasn't
quickly adopted until Java 9 and 10 came
out when they introduced the additional
supplement methods to help reduce the
overhead
but what it allows for is you can now
chain or do method chaining instead of
having to do all the nasty if null
checks to verify if a particular method
or variable
potentially can be null it also implies
what the actual intent is of the
variable or the method
basically if optional is there that
tells you that you need to be conscious
that there could potentially be a null
value
the value may or may not be instantiated
okay got it
with that I'd like to thank everyone for
your time
if you'd like to discuss any of these
topics further or if you have any
additional topics you would like us to
cover please reach out to us
we're available through email at info
developmenter.com you can send us a
message on our website developmenter.com
contact us
you can find us on YouTube search for
YouTube developer
or use the developer tag
we are also available on Vimeo vimeo.com
developer
Facebook also facebook.com developer
Twitter same twitter.com developreneur
our goal is making every developer
better have a great day
[Music]
[Music]
[Music]
foreign
[Music]