📺 Develpreneur YouTube Episode

Video + transcript

Java Optional Example 1

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

foreign

4.49

[Music]

16.62

[Music]

22.02

so let's start with example one so we

25.08

are going to create an empty optional

27.3

object we are going to start by looking

30.599

at using the empty method then we're

33.48

going to after we create our object

35.399

we're then going to check to see if the

37.62

optional is empty and then we're going

40.5

to check to see if the optional is

42.48

present

43.92

all right so let me jump out of the

45.66

slides

47.82

all right so our first example

50.76

we're going to start by creating an

53.16

empty optional object now I've went

56.52

ahead and created these four example

58.92

classes as a unit tests so we're going

61.92

to use the assert J the assert that

64.979

class from the assert Date Presentation

67.2

to kind of quickly show some comparisons

69.78

and validate what we're doing

72.06

so the first one here we're going to

73.979

create an object that create an empty

76.68

optional object

78.18

he had some spaces here so we can read

80.52

this

82.08

all right so the first thing we need to

83.7

do is we need to create an optional

85.86

object so you start with the optional

91.92

so we start with the optional class

93.36

which if we note is in the Java util

96.36

optional

99.72

and it takes some type of generic so we

102.6

have to tell it what type of optional we

104.579

want to create so for simplicity's sake

107.1

let's just use our string class

114.119

and let's just call this empty

118.02

optional

121.619

string

126.079

and to create an empty optional you use

130.8

the optional class again

135.18

and you don't need to type it and we can

139.2

use the empty method which returns an

142.8

empty optional instance no value is

145.08

present for option

146.819

so if we use empty

149.819

so no value is present for this option

152.64

all right so this is how we create an

155.76

optional object and if we read it we

159.06

understand the intent so we have an

161.22

optional string so this string value

164.04

empty optional string

166.26

potentially could be empty

169.739

in our particular case because we called

172.76

optional.empty we know it is empty so I

175.86

named it empty optional string

178.319

so again our naming Convention of our

180.9

methods and our variables help to imply

183.599

the intent of what the code is doing

187.08

all right so second let's now do an

190.14

assertion to check to see if that

192.12

optional is empty now to check if the

195.06

optional is empty we again use the empty

199.019

optional string that we just created but

202.019

in this case we're going to use the is

204.36

empty method

208.2

and because this is an optional empty

211.56

implementation here this should return

213.959

true

214.98

so to do a simple assert test we can do

218.22

assert that

220.56

so we could do this two ways one we can

223.44

use the assert that

225.54

and we can check that it's empty or we

228.42

can do it this way we can take a certain

230.76

empty

231.84

is empty

233.519

however this will simply return true or

236.94

false but if we want to show intent

239.519

which a certain J does we want to say

242.22

okay we want to assert this is something

245.58

so we follow this with DOT is

250.56

equal to

252.299

true

254.22

so now with the search J we read this as

256.799

we want to assert that in our empty

259.62

optional string is empty is true

263.699

so if we run this

267.419

we pass

269.82

so good

272.04

so empty optional string is empty

275.24

returns true because we have an empty

278.82

optional declaration here

282.24

all right so now how can we check to see

284.82

that the optional is not present so we

287.34

can check that it's empty but we can

289.56

also check to see if it's not present

294.72

so if it's present

296.88

that means that it's not empty

299.58

so if we do is present

304.139

we'll return a value if present it will

307.44

return true otherwise false so if we do

310.8

is present

312.66

this should be false

316.08

and we pass

318.6

all right so for example one we want to

320.639

create an empty optional object to do

323.4

that we start with optional

326.759

we then set the generic for the object

328.979

type that we want so in this case string

331.46

we named it empty optional string and

334.86

then we instantiated with the optional

336.96

dot empty method which just returns an

339.78

empty optional instance no value is

342

present

343.38

then we check to see if the optional is

345.66

empty so we took our variable we called

348.66

the is empty method which will return

351.6

true because our optional is empty

356.759

and then lastly for example one we want

359.52

to check to see if the optional is not

361.919

present so we again used our optional

365.58

variable and we used the method is

367.919

present to verify that the optional is

372.479

not there that it is empty so if a value

374.82

is present we would get true otherwise

377.28

we got false which is what we expected

385.64

[Music]

397.88

thank you