📺 Develpreneur YouTube Episode

Video + transcript

Java Optional Example 2

2023-01-31 •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]
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
1.02

foreign

5.16

[Music]

16.6

[Music]

21.6

for example two we are going to create

24.06

an optional object from an object

27.42

so we're going to actually create an

29.279

object first and then convert it to an

31.38

option

32.34

we're going to look at some of the

34.86

caveats or things we need to be careful

36.96

of when we use the of method and then

39.48

we're going to create an optional with

41.1

an object that could be null

44.7

so in example two we want to create an

46.86

optional object from an object so we

49.02

need to start with an object

51.48

so for that I will

53.579

create a string so let's start with a

56.52

string

59.879

and let's call it uh onion

64.519

and we'll just name it onion or

67.08

Simplicity all right so now we have a

69.36

string variable onion with a string

72.479

value of onion

74.7

now to convert this to an optional again

77.64

we start with optional

80.34

we take the generic type for string

84

and we're going to call this one our

86.52

optional

87.84

string

91.32

Now to create or I should say to convert

94.619

this string object to an optional object

98.34

we use the of method of the optional

101.939

class so use optional Dot

105.24

of and of returns an optional describing

109.56

the given non-null value

113.22

so now if we do onion

117.299

we are happy so optional

120.06

string so optional string our string

123

could be it null of onion so we're going

126.119

to create our optional

128.64

from our string object here onion

132.42

all right let's make sure that we have a

134.64

value so now we can do assert

137.819

that

141.72

optional string

145.56

Dot

147.959

is present

151.68

Dot

154.44

equals

157.62

to I'm sorry is equals to

161.519

now that we have our optional string of

163.62

optional we're going to assert that

165.66

optional securing is present which

168.54

should return true

170.48

I like using the assert that's because

172.739

this is very readable all right so we

174.78

create an object optional object from an

177.12

object string onion

179.64

we convert it to an optional object

182.72

optional type

184.92

and then we verify that that option

187.8

string was stored as an option so if we

191.94

save this

193.2

run as

195.9

and we pass so in this case optional

199.62

string is present

204.54

now

206.519

what happens if we do this slightly

209.58

differently so what if we did a string

213.239

no string

217.44

and we have a string value of null well

220.92

of is not going to like this of will

223.62

actually throw a runtime exception so to

227.34

test that to verify that we will do

229.739

assert

231.959

so we'll assert that exception

236.099

of type so we're expecting a null

239.28

pointer exception

243.42

is thrown by

248.28

do a function call here all right so

250.799

we'll take optional

252.299

so we're going to do the same thing here

253.86

optional of

257.16

and we're going to take our null string

268.259

so we're asserting that we should get an

270.9

exception of no pointer exception when

274.38

we try to set our null string to an

278.22

optional

279.36

so if we run this

284.1

that is the case

286.979

so how do we fix this how do we avoid

289.32

this

291.6

well

292.8

with the introduction of

295.56

I think Java 9 maybe Java 10. they

299.699

changed this so they added an option

302.58

that allows us to create a nullable

305.52

option so for that we start with a new

310.32

optional string

316.44

foreign

318.32

so this will be an option

322.02

with null

328.02

string

330.12

and we can set it equal to

334.139

optional

336.539

dot of nullable and this will return an

340.08

object describing the given value if not

343.56

null

344.639

otherwise it will return an empty

347.22

optional so this handles the null

349.44

pointer Exception by converting it to an

352.139

empty optional so we do all nullable and

355.979

then here we can pass in our null string

362.28

so now we should not get a null pointer

365.16

exception instead our option with null

368.58

string should return an empty nullable

372.18

or empty uh optional so let's check that

376.199

so let's assert

378.9

that

382.44

our option

385.139

with no string

389.58

Dot is empty

396.9

Dot is equal to

400.62

true

405.419

so what we're doing here is we're going

407.039

to check to see if the option optional

409.86

we created here with the nullable string

412.86

is empty and that it returns true so

416.94

we're checking to make sure that this

418.259

guy

418.979

is null or is it it returns empty when

423.36

null instead of throwing the null

425.52

pointer exception so if we run this

429.78

it passes

433.979

all right so for example two we started

436.139

out by creating an optional object from

438.24

an object we started with the string

440.699

onion

442.02

with some text onion we then created an

445.74

optional variable and instantiated our

449.16

optional string with our string onion

452.4

using optional of

455.099

we looked at being cautious of using

457.979

optional of with nulls so if you pass in

461.039

a null object and try to convert it to

462.66

nullable with of you will get a null

465.24

pointer exception

466.86

however optional did add an additional

470.099

method to handle nulls called of

473.94

nullable so if you declare a string or

477.9

declare an object as nullable or an

481.38

object up to option and it is Noble you

484.5

need to use of nullable

492.24

[Music]

504.379

thank you