📺 Develpreneur YouTube Episode

Video + transcript

Java Optional - The Presentation

2023-01-24 •Youtube

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
1.02

foreign

5.16

[Music]

16.6

[Music]

24.77

[Music]

35.1

foreign

36.16

[Music]

43.76

optional class so this is just going to

46.379

be an introduction to the optional class

48.48

in Java

51.059

we're primarily going to be doing an

53.28

overview of the optional class so we're

55.14

going to look at what is it why is it

57.239

useful and how do we use

60.84

so let's start with what it is so the

63.899

optional class is essentially a

66.299

container object which may or may not

69.119

contain a non-nullable value

73.08

so why is this useful well one of the

75.78

biggest problems with Java is even from

78.54

the the day it was created is the intent

82.38

of our methods the intent of our

85.32

variables is not really clear if we're

88.74

going to have a value or not so we have

92.1

an example here string name

94.32

stirring get name and then some method

96.72

that actually tries to compare a string

101.22

name equal to our get name which should

104.159

return the name

105.96

the problem with this is name could be

109.14

null and throw a null pointer exception

111

at runtime

112.259

so just reading the code we don't know

114.84

for instance if name potentially could

117.899

contain a null value so it's very

120.119

unclear on the intent

123.479

so how do we know the name could be null

125.399

we don't because name is just specified

128.039

as name

129.479

unfortunately this approach leads up to

131.76

the developer to determine the intent of

134.879

the variable if it can be null so we

136.86

then have to add a lot of null checks

138.72

within our application

140.64

so is name equal to null his name not no

145.14

and what do we want to do with it

147.9

if we don't then we're going to get

149.52

runtime exceptions all over the place

152.16

and this is where optional comes in

156.18

so why is optional useful so the

159.18

optional class now gives us the ability

162.18

to wrap our strings or wrap our

165.72

variables and wrap our methods with a

169.92

optional class which basically implies

173.64

the intent that our variable or our

176.4

method could potentially return a null

179.4

result or a null but optional in and of

183.12

itself it handles the notes so optional

186.12

basically says okay name is optional

189.18

meaning name might not have a value

192.659

in this particular case

194.76

names not going to return null because

197.519

the optional class has some methods that

200.22

handle How We Do null pointers or null

203.099

checks

204.3

same with our methods here our optional

206.94

string

208.44

so we create a string a return type

210.72

forget name

212.4

so again we're specifying that name May

216.12

return no result so it could potentially

219.12

be blank but because we're doing

221.34

optional we're going to know that we

223.739

need to handle that no or we need to at

226.68

least filter out that in all value

230.04

so this is going to be a very Hands-On

233.58

driven presentation today

235.86

so we've got four examples with the

238.98

first example being we're going to

241.019

create an empty optional object so we're

244.14

going to look at the basics of optional

245.7

how to create the objects how to check

248.4

if the optional is empty and how to

250.98

check if the optional has a value

254.879

our second example we're going to go a

258.6

little bit deeper into the optional

260.579

methods we're going to look at how to

263.34

create an optional object from an object

266.1

so we can take that string and actually

268.68

convert it to an optional we're also

271.08

going to look at some of the issues of

273.84

using the of method which can't be null

277.139

this will throw an old pointer exception

278.94

so we'll look at some caveats and things

281.52

to watch out for of the basics of using

284.1

some of these simple methods

286.02

and then the third piece we're going to

287.88

look at in the second example is we're

290.04

going to look at creating an optional

292.139

with an object that could be known so

295.139

we're going to look at converting an

297.24

object to an optional we're going to

299.58

look at some of the issues of of how we

302.639

could get a null pointer exception and

305.1

then we're going to look at creating an

307.56

optional with nullable

310.44

so we're actually going to show how to

312.18

create an optional showing the intent

314.699

that it could contain nullable values

318.66

for our third example we are going to

321.72

dive deeper into the optional class and

324.06

actually start looking at some of the

325.8

conditional options that we have with

328.86

the optional class

330.78

so we're going to look at how to get our

333.18

object out of the optional so we're

335.699

going to look to see how we can get that

337.38

string value out of our optional string

340.8

we're going to look at comparisons

342.18

between null checks versus the optional

344.46

if present method

346.38

we're also going to look at the if

348.539

present or else which was introduced in

350.639

Java 9.

352.02

and then we're going to look at some of

353.639

the other conditional methods as well

355.62

like or else or else get or else throws

360.259

and with Java 10 they introduced a

363.24

simplified version of the or else which

365.4

just throws no such element exception

369.3

and for our last example example four

372.18

we're going to look at using the

374.52

streamed methods to map our optional

378

objects into their object counterparts

381.9

so this could be useful for jpas for

384.18

entities for or just plain old objects

387.36

we can also using the filter method to

390.24

actually remove nulls or pull out the

392.4

specific data that we want from within

394.74

our option or within our streams

399.06

very useful and we're also going to look

401.039

at using multiple filters to pull out

403.68

specific values

406.02

so you can almost look at filter like a

408.66

where clause in a SQL statement very

411.66

cool with streams

413.52

and then lastly we'll look at the flat

416.039

map method

417.3

which basically allows you to take a

419.46

flattened list of streams or flattened

423

list of objects and map them out in a

426.84

flat map option

430.86

any questions or comments

435.36

is Java option used

439.199

so Java options is so as you saw where I

442.62

was using the streaming there in the

444.24

last example

445.8

when streams were introduced in Java 8

449.28

they were introduced through optional

451.58

and early on it wasn't

454.86

quickly adopted until Java 9 and 10 came

458.16

out when they introduced the additional

460.68

supplement methods to help reduce the

462.72

overhead

464.039

but what it allows for is you can now

467.28

chain or do method chaining instead of

469.919

having to do all the nasty if null

473.639

checks to verify if a particular method

477.9

or variable

479.46

potentially can be null it also implies

483.539

what the actual intent is of the

487.319

variable or the method

489.3

basically if optional is there that

492.06

tells you that you need to be conscious

494.759

that there could potentially be a null

496.979

value

498.24

the value may or may not be instantiated

503.28

okay got it

508.199

with that I'd like to thank everyone for

510.18

your time

511.5

if you'd like to discuss any of these

513.599

topics further or if you have any

516.24

additional topics you would like us to

517.919

cover please reach out to us

520.56

we're available through email at info

522.839

developmenter.com you can send us a

525.48

message on our website developmenter.com

527.16

contact us

529.019

you can find us on YouTube search for

531.779

YouTube developer

533.519

or use the developer tag

536.279

we are also available on Vimeo vimeo.com

539.16

developer

541.019

Facebook also facebook.com developer

544.92

Twitter same twitter.com developreneur

549.36

our goal is making every developer

551.1

better have a great day

558.88

[Music]

570.32

[Music]

578.44

[Music]

587.339

foreign

589.88

[Music]