Optionals In Java - Simple Tutorial

Coding with John
27 Jun 202215:53

Summary

TLDRThis video tutorial explains the concept of Optionals in Java, emphasizing their correct usage to handle potential null values. It illustrates Optionals as containers that may or may not hold a value, contrasting them with traditional null checks. The video demonstrates how to use Optional methods like 'isPresent', 'get', 'orElse', and 'orElseGet' to safely access values or provide defaults, avoiding NullPointerExceptions. It advises using Optionals primarily as return types to signal the possibility of a null return, rather than overusing them.

Takeaways

  • 😀 Optionals in Java are used to handle the absence of a value where a method might not have a value to return.
  • 🛠 The primary purpose of Optional is to explicitly signal that a method may not return a value, avoiding null pointer exceptions.
  • 🏢 Sponsored content features Flexispot, highlighting their ergonomic office furniture, particularly their standing desks.
  • 🐱 An example is given using a method that retrieves a Cat object from a database, which may or may not exist, thus returning an Optional.
  • 📦 Optionals can be thought of as a box that might contain a value or be empty, indicating the possible absence of a result.
  • 🔄 The method `Optional.ofNullable` is used to create an Optional that might contain a value or be empty.
  • 🚫 Using `Optional.get()` without checking `Optional.isPresent()` first can lead to a `NoSuchElementException`, similar to a `NullPointerException`.
  • 🔄 Methods like `orElse` and `orElseGet` provide safer ways to retrieve values from an Optional without risking exceptions.
  • 🔄 `orElseThrow` behaves like `get`, throwing an exception if the Optional is empty, which is discouraged in favor of safer methods.
  • 🔧 The `map` method allows Optionals to be transformed into another type, providing a way to extract or compute values safely.
  • ⚠️ Optionals should be used sparingly and specifically as a return type to indicate the potential absence of a value, not just anywhere a variable might be null.

Q & A

  • What is the main purpose of using Optional in Java?

    -The main purpose of using Optional in Java is to handle situations where a method might not have a value to return, thus avoiding null pointer exceptions and explicitly informing the user that the value might not exist.

  • How does the sponsor Flexispot relate to the content of the video?

    -Flexispot is mentioned as the sponsor of the video, providing high-quality, reasonably priced ergonomic office furniture, with a focus on standing desks. The sponsor's product is showcased as the E7 Pro Plus standing desk, which the presenter is using during the recording.

  • What is the difference between using Optional.of and Optional.ofNullable?

    -Optional.of is used when you are certain that the object is not null, whereas Optional.ofNullable can handle both null and non-null values, returning an empty Optional if the value is null.

  • Why might using the 'get' method on an Optional be problematic?

    -Using the 'get' method on an Optional can be problematic because if the Optional is empty, it will throw a NoSuchElementException, which is similar to a NullPointerException, thus defeating the purpose of using Optional.

  • What is the significance of the 'orElse' method in Optional?

    -The 'orElse' method returns the value inside the Optional if it contains one; otherwise, it returns a default value that you provide as a parameter, preventing exceptions when the Optional is empty.

  • How does the 'orElseGet' method differ from 'orElse'?

    -The 'orElseGet' method allows you to provide a lambda expression or supplier function to generate a default value if the Optional is empty, whereas 'orElse' requires a direct value.

  • What does the 'orElseThrow' method do in Optional?

    -The 'orElseThrow' method returns the value inside the Optional if it contains one; if the Optional is empty, it throws a NoSuchElementException, similar to what the 'get' method does.

  • Can you provide an example of how the 'map' method is used with Optional?

    -The 'map' method is used to transform the value of an Optional into another type. For instance, if you have an Optional of a Cat object, you can use 'map' to transform it into an Optional of an Integer representing the Cat's age.

  • What is the recommended way to handle Optionals according to the video?

    -The video recommends using Optionals as a return type to indicate that a method might return null. It advises against overusing Optionals and suggests using methods like 'orElse', 'orElseGet', and 'orElseThrow' instead of the 'get' method.

  • Why might returning an Optional be considered better than returning null?

    -Returning an Optional is considered better than returning null because it makes the possibility of a null value explicit, forcing the caller to handle the absence of a value, thus reducing the risk of NullPointerExceptions.

  • What is the role of赞助商Flexispot在视频中提到的促销活动?

    -赞助商Flexispot提到了他们的Prime Day Sale,在6月29日和30日,他们的产品最高可以享受100美元的折扣。此外,通过视频描述中的链接购买,还可以额外享受30美元的折扣。

Outlines

00:00

📘 Introduction to Java Optionals

The paragraph introduces the concept of Optionals in Java, explaining that they can be challenging to understand and are often misused. The video aims to clarify what Optionals are, when they should be used, and how to use them correctly. It begins with a sponsored segment for Flexispot, highlighting their ergonomic office furniture, particularly their standing desks. The E7 Pro Plus model is showcased for its capacity to hold heavy loads and its adjustable height range. The script then transitions into explaining Optionals as a container that may or may not hold a value, using a database retrieval example to illustrate the problem Optionals aim to solve, which is the potential for null values and the NullPointerException that can result.

05:01

🔍 Understanding and Using Optionals

This section delves into how to use Optionals in Java. It explains that Optionals can be thought of as a box that might or might not contain a value, emphasizing the need to handle the possibility of an empty Optional. The script walks through changing a method's return type to Optional and using the 'ofNullable' method to create an Optional from a potentially null object. It also discusses the common mistake of using the 'get' method without checking if the Optional is empty, which can lead to a NoSuchElementException. Instead, it suggests using 'isPresent' in combination with 'get', or better yet, the 'orElse' method for a more elegant handling of empty Optionals.

10:01

🛠️ Advanced Optional Methods

The paragraph explores advanced methods provided by the Optional class in Java. It introduces 'orElse', which returns the value inside the Optional if present or a default value if empty. The 'orElseGet' method is also mentioned, which allows for the provision of a lambda expression to supply a default value if the Optional is empty. The paragraph then discusses 'orElseThrow', which throws a NoSuchElementException if the Optional is empty, similar to the 'get' method. It also touches on the 'map' method, which can transform an Optional of one type to another, and how it can be used to extract values like the age of a cat from the database without directly dealing with null checks.

15:02

🚩 When to Use Optionals

The final paragraph addresses when to use Optionals, clarifying that they are intended primarily as a return type for methods that might not return a value. It advises against overusing Optionals and reiterates that they are meant to signal to the method's user that a returned value might be null. The script concludes with a call to action for viewers to like, subscribe, and check out the full Java course for more in-depth tutorials.

Mindmap

Keywords

💡Optionals

Optionals in Java are a container object that may or may not contain a non-null value. They were introduced to provide a more elegant solution to null pointer exceptions. In the video, optionals are used to handle situations where a method might not return a value, such as when searching for a cat by name in a database. The script illustrates creating an Optional of Cat, checking if it contains a value with 'isPresent()', and retrieving the value with 'get()' if present.

💡Null Pointer Exception

A Null Pointer Exception occurs when you try to access a method or property on an object that is null. The video script discusses how using Optionals can prevent this exception by explicitly indicating that a method might return an empty Optional instead of a null value, thus avoiding the need to call methods on a null reference.

💡Flexispot

Flexispot is a company that manufactures ergonomic office furniture, including standing desks. In the video script, Flexispot is mentioned as the sponsor of the video, and the presenter discusses the features of their E7 Pro Plus standing desk, highlighting its capacity to hold heavy loads and its adjustable height range.

💡Standing Desk

A standing desk is a type of desk that allows you to alternate between sitting and standing while working. The video script features a standing desk from Flexispot, emphasizing its benefits for ergonomics and how it can be customized with various features like height presets and a USB charging port.

💡E7 Pro Plus

The E7 Pro Plus is a specific model of standing desk from Flexispot. The script describes it as having a carbon steel frame and dual motor design, capable of holding up to 355 pounds, making it suitable for various devices and even a set of adjustable dumbbells.

💡Lambda Expressions

Lambda expressions are a way to represent anonymous functions in Java. The video script touches on using lambda expressions with Optionals, specifically in the context of the 'orElseGet' method, which allows for the provision of a default value through a lambda if the Optional is empty.

💡orElse

The 'orElse' method is part of the Optional class in Java. It is used to return the value inside the Optional if it exists, or a default value if it is empty. The script uses 'orElse' to provide a default cat with an age of zero when the Optional Cat is empty.

💡orElseGet

The 'orElseGet' method is similar to 'orElse' but allows for a supplier function to provide the default value if the Optional is empty. This method is mentioned in the script as an alternative to 'orElse' for those comfortable with lambda expressions.

💡orElseThrow

The 'orElseThrow' method is used when you want to throw an exception if the Optional is empty. The script points out that 'orElseThrow' behaves the same as the 'get' method, throwing a NoSuchElementException if there is no value present.

💡map

The 'map' method in Optional allows for transforming the value contained within the Optional into another type. The script demonstrates using 'map' to transform an Optional Cat into an Optional Integer representing the cat's age, providing a way to safely extract the age without directly calling 'get'.

💡isPresent

The 'isPresent' method checks if there is a value present in the Optional. If true, it means the Optional contains a non-null value. The script uses 'isPresent' to conditionally check if an Optional Cat has a value before attempting to retrieve and print its age.

Highlights

Optionals in Java are difficult to understand and often misused.

Optionals should be used when a method might not have a value to return.

Flexispot赞助视频,提供高质量的办公家具,特别是站立式办公桌。

E7 Pro Plus站立式办公桌具有碳钢框架和双电机设计,承重高达355磅。

站立式办公桌具有广泛的调节高度范围和四个高度预设。

Optional作为容器,可能包含值,也可能为空。

Optional用于显式告知方法的使用者可能不存在期望的值。

Optional.ofNullable方法用于创建包含特定值的Optional。

Optional.empty方法用于显式创建一个空的Optional。

Optional.get方法用于获取Optional中的值,但可能抛出NoSuchElementException。

Optional.isPresent方法检查Optional是否包含值。

Optional.orElse方法提供了一种优雅的处理Optional为空的方式。

Optional.orElseGet方法结合Lambda表达式提供默认值。

Optional.orElseThrow方法在Optional为空时抛出异常。

Optional.map方法用于将Optional转换为另一种类型的Optional。

Optional应该仅用作返回类型,以明确方法可能返回null。

Optional不应该被过度使用,只在适当的情况下使用以避免空指针异常。

Transcripts

play00:00

in this video we'll talk all about

play00:01

optionals in java optionals are tough to

play00:04

understand and even when developers do

play00:06

understand them they're constantly using

play00:07

them the wrong way so we're going to

play00:09

clear up exactly what they are when they

play00:11

should be used and how to use them

play00:13

before that this video is sponsored by

play00:15

flexispot flexispot makes high quality

play00:18

reasonably priced ergonomic office

play00:20

furniture and is probably best known for

play00:22

their standing desks they have a

play00:23

ridiculous number of desks to choose

play00:25

from each with tons of customization

play00:27

options this one is the e7 pro plus

play00:30

standing desk i'm recording on it

play00:32

right now

play00:34

its carbon steel frame and dual motor

play00:36

design make it capable of holding up to

play00:39

355 pounds so you can keep all of your

play00:41

work school and gaming devices and 100

play00:44

pounds set of adjustable dumbbells on

play00:46

top with no worry it has a crazy height

play00:49

range from 22.8 inches all the way to

play00:52

48.4 inches i'm six foot five and it

play00:54

goes even higher than i need it to you

play00:57

can program in four height presets to

play00:59

quickly go from sitting to standing with

play01:01

a single touch there's even a usb port

play01:03

right here on the side of the keypad so

play01:05

you can conveniently charge your devices

play01:07

i use that all the time the e7's quality

play01:10

and smart design make it the best

play01:12

ergonomic desk choice for your home

play01:14

office be sure to check it out through

play01:15

my link down in the description because

play01:17

you'll get an extra thirty dollars off

play01:18

your purchase plexi spot is also having

play01:20

their own prime day sale on june 29th

play01:23

and 30th with up to a hundred dollars

play01:25

off their products so start customizing

play01:27

your own standing desk through my link

play01:28

down in the description to get that

play01:30

extra 30 off and i know you'll love it

play01:32

as much as i do now let's get to it

play01:34

first just what exactly is an optional

play01:37

all an optional is is a container that

play01:40

either has something in it or doesn't

play01:42

that probably sounds super weird right

play01:44

what's the point of that so let's start

play01:46

right away with an example let's say

play01:48

that we had a simple method in our

play01:50

program that took in the name of a cat

play01:52

as a parameter and then went to the

play01:54

database and retrieved a cat object with

play01:58

that name and returned it so that would

play02:00

be just private static cat because we

play02:03

want to return a cat and we'll call this

play02:05

method find cat by name and we'll take

play02:08

in as a parameter that string name for

play02:11

the cat that we're looking for now in a

play02:13

real application this would actually

play02:15

fetch a cat from the database but here

play02:17

we're just going to create a cat object

play02:19

with that name and return it so to do

play02:21

that we'll just say cat cat equals new

play02:24

cat now this constructor takes the cat's

play02:26

name as the first parameter so we'll

play02:28

just enter in the name that's passed

play02:30

into this method and it takes the cat's

play02:33

age as the second parameter so we'll

play02:34

just give it the age of three and then

play02:36

all we have to do is return that cat so

play02:39

now back here in our main method we can

play02:41

call this fine cat by name method and we

play02:44

can pass in the name of the cat that we

play02:46

want to find so let's just pass in bread

play02:49

so that method will of course return the

play02:51

cat it retrieved from the database and

play02:53

if we want we can store it in variable

play02:55

so let's do that cat my cat equals the

play02:59

result of this method call so now of

play03:01

course if we want we can print out the

play03:03

age of the cat that it retrieved so

play03:05

print out my cat dot get age and of

play03:08

course if we run it that all works fine

play03:10

and it prints out three but what if

play03:12

there is no cat named fred in the

play03:15

database in that case what should a

play03:18

method like this return well it probably

play03:20

makes sense to just return null right if

play03:23

that cat doesn't exist it doesn't make

play03:25

sense to return a cat it just makes

play03:27

sense to return null but now we have a

play03:29

problem this method is now returning

play03:32

null which means that we're now trying

play03:34

to call the get age method on a null

play03:37

variable and of course if we try that we

play03:39

will get a no pointer exception you see

play03:42

this kind of situation all the time and

play03:44

the only way we can really handle this

play03:46

situation is with a null check just

play03:48

something like if my cat does not equal

play03:51

null then go ahead and print out its age

play03:55

otherwise maybe we print out some

play03:57

default age

play03:58

like zero so this is the way you had to

play04:01

handle this type of situation for a long

play04:03

time in java this whole situation is

play04:05

where optionals come in optionals are a

play04:08

better way to handle a situation where a

play04:11

method like this might not have a value

play04:13

to return so here's the idea instead of

play04:16

returning an actual cat object or null

play04:19

it instead returns

play04:21

an optional an optional is like this box

play04:24

it either contains the cat or it doesn't

play04:26

so basically now what our method will do

play04:28

is if it finds a cat with that name in

play04:30

the database it will put that cat in the

play04:33

optional box

play04:35

and return it and if it doesn't find

play04:37

anything it will just return the empty

play04:39

box and then the code that is processing

play04:41

this return value can ask this optional

play04:44

box whether it contains a value or not

play04:46

and then do the appropriate thing if it

play04:48

does or doesn't so really the main

play04:51

purpose of an optional is to explicitly

play04:53

tell the user of a method that the value

play04:56

that they're looking for might not exist

play04:59

and they have to account for that

play05:00

possibility so maybe instead of just

play05:02

thinking of optionals as a box you can

play05:05

instead think of it as a box that says

play05:07

hey i might not have a value in here so

play05:09

you have to be prepared to handle that

play05:11

situation so now that we know that we

play05:13

want to return an optional of a cat here

play05:16

in our method instead of just a cat

play05:17

object how exactly do we go about doing

play05:20

that so all we have to do is instead of

play05:22

having cat as our return type we'll

play05:24

instead have optional of cat so we just

play05:27

have optional and then cat in angle

play05:30

brackets and here we have this cat

play05:32

object to return but we have to put that

play05:34

cat inside the optional box first the

play05:37

easiest way to do that is with this

play05:39

method here

play05:40

optional.of nullable and then you just

play05:43

pass into this method as a parameter the

play05:46

thing you want to create an optional of

play05:48

which here is cat now this value that

play05:51

you pass into the of nullable method can

play05:54

either be null or not if it's not null

play05:56

it'll just put that value into the

play05:58

optional as we said but if it is null

play06:00

like if this cat didn't exist in the

play06:02

database it will instead just create an

play06:05

empty optional as a side note if you

play06:07

know for sure that the thing that you

play06:10

want to put into this optional is not

play06:12

null then you can instead just use

play06:15

optional dot of but if you pass in

play06:17

something that is null to this method

play06:19

you're going to get an exception so if

play06:21

you're not sure whether or not the thing

play06:23

that you're passing in is null just go

play06:25

ahead and use of nullable also if you

play06:28

ever want to explicitly create an empty

play06:31

optional you can just use optional dot

play06:34

empty anyway now up here of course we're

play06:37

not just getting a cat object anymore

play06:39

we're getting an optional of cat

play06:42

returned from this method call so we

play06:44

have to change this to optional of cat

play06:47

and we'll change the name to optional

play06:50

cat so now instead of having just a cat

play06:52

we have an optional that may or may not

play06:55

contain a cat so how do we actually get

play06:57

the cat out of this optional first let

play06:59

me show you how basically everybody does

play07:01

it when they first use optionals so

play07:04

there is a method on each optional just

play07:07

called get and the get method will

play07:09

return the value that's inside the

play07:11

optional so you can just use this get

play07:14

method which will return our original

play07:16

cat object and then we can call get age

play07:19

on it and of course like before if we

play07:21

want we can go ahead and print that out

play07:24

and it works however if there is no

play07:27

value in this optional to get so that's

play07:30

like the case if this method didn't find

play07:33

that cat in the database it might create

play07:36

an optional of null so this will return

play07:39

an empty optional and if you try to call

play07:42

git on an empty optional

play07:44

you get

play07:45

a no such element exception which will

play07:48

ruin your day so that's a problem right

play07:50

it's just as bad as a null pointer

play07:52

exception so then most developers figure

play07:54

that out pretty quickly okay i have to

play07:56

do a check on this optional to see if

play07:59

there's a value in it before i call dot

play08:01

git but luckily there's a method that

play08:03

you can call on the optional to check

play08:06

whether there's a value present so we

play08:07

can just call optional cat dot is

play08:10

present is present will return true if

play08:12

there is a value in the optional and

play08:14

false if it's empty so we can set things

play08:16

up like this uh if optional cat dot is

play08:19

present then go ahead and print out the

play08:22

age otherwise like we did before we'll

play08:25

go ahead and print out some default like

play08:27

zero and we can go ahead and run it and

play08:30

it's all good it prints out the default

play08:31

of zero if this optional was empty well

play08:34

perfect that settles it

play08:36

right

play08:37

except hey

play08:38

this whole setup looks suspiciously

play08:41

exactly like the null check that we were

play08:44

trying to get away from in the first

play08:45

place so what's the deal right all of

play08:48

that work just to get something that

play08:50

looks almost exactly like we had before

play08:52

so there's a couple of things here first

play08:55

remember that the primary reason for

play08:57

returning an optional in a method like

play08:59

this is to blatantly convey to the user

play09:02

of this method that it might not return

play09:05

a value that they're looking for and

play09:07

make sure that they know that they have

play09:09

to deal with that situation so even if

play09:11

the end result did look similar it's

play09:13

still making that clear to the user

play09:16

just by returning an optional in the

play09:18

first place but also optional offers

play09:21

some other nice methods to use instead

play09:23

of using this is present get combination

play09:26

and actually the people who created java

play09:28

optionals kind of regret that they even

play09:31

offered this get method in the first

play09:33

place because it's just so easy for

play09:35

people to call something that looks so

play09:37

harmless as dot get without checking is

play09:40

present first so i think when you're

play09:42

first learning the concept of optionals

play09:45

and how to work with them it's fine to

play09:47

use this is present and get combination

play09:50

just to get the feel of things as long

play09:52

as you are always checking is present

play09:54

every time before you call git but once

play09:57

you get more familiar with them there

play09:59

are nicer methods you can use to work

play10:00

with them one of the methods that's

play10:02

commonly used is actually called or else

play10:06

kind of a weird name for a method right

play10:08

almost sounds like a threat optional cat

play10:11

or else but what this method does is

play10:13

kind of interesting so if this optional

play10:16

contains a value it will just return

play10:18

that value but if this optional is empty

play10:22

this method will instead return the

play10:25

value that you pass in here as a

play10:27

parameter you can kind of think of it as

play10:29

a default so we could have something

play10:30

like cat my cat equals optional cat or

play10:35

else so maybe as our default cat we

play10:38

could say new cat and just give it a

play10:40

name of unknown and an age of zero so

play10:44

what this will do is if this optional

play10:46

contains a cat it will just return it

play10:48

exactly the same way that the get method

play10:51

did however

play10:53

or else if the optional is empty instead

play10:56

of just blowing up this method will

play10:58

return this default unknown cat with an

play11:02

age of zero so that's a pretty elegant

play11:04

solution right it gives you the value if

play11:06

it's there but if it's not it doesn't

play11:08

ever throw an exception or anything it

play11:10

just provides you the default value

play11:12

another method that's available is

play11:14

called or else get if you're comfortable

play11:17

with lambdas and check out this lambdas

play11:19

video if you're not you can use this

play11:20

method and pass in a lambda supplier

play11:23

function so with this method if the

play11:25

optional contains a value as before it

play11:27

will just return it but if the optional

play11:29

is empty it will use your lambda method

play11:32

to supply the default value to return

play11:35

again if you aren't super comfortable

play11:36

with lambdas yet don't worry too much

play11:38

about this method but if you are it's

play11:40

good to know about ironically there's

play11:42

also this method called or else throw

play11:45

now what this method does is if the

play11:47

optional contains a value it just

play11:49

returns it but if the optional is empty

play11:51

it will throw a no such element

play11:53

exception now if that sounds familiar to

play11:56

you it's because that's exactly the same

play11:58

thing that the dot get method does those

play12:01

two methods do exactly the same thing

play12:03

there is no difference in fact in the

play12:06

javadocs for git so if you hover over

play12:09

get here in the ide it tells you api

play12:12

note the preferred alternative to this

play12:14

method is or else throw but here's an

play12:17

example of some of the real power of

play12:19

optionals notice that in our situation

play12:22

we were mainly interested in just

play12:24

getting the age of the cat that was

play12:26

pulled back from the database right well

play12:28

we can actually do that with optionals

play12:31

with just a couple of chained method

play12:33

calls so what we can do is take our

play12:35

optional cat and then call a method on

play12:37

it called map what this map method

play12:40

allows us to do is to take our cat

play12:43

optional and transform it into an

play12:46

optional of another type and it does

play12:48

that with a method that we pass in as a

play12:50

parameter that may sound a bit weird but

play12:53

let me show you what i mean so we can

play12:54

actually pass into this method cat colon

play12:58

colon get age

play13:00

again if you're not really used to

play13:01

lambdas yet this looks a little weird

play13:03

but what this method call will return is

play13:06

an optional containing the result of

play13:09

calling the getage method on the cat

play13:12

that was in our original cat optional if

play13:15

our original cat optional was empty then

play13:18

the result of this method call will just

play13:19

be an empty optional as well and it

play13:21

won't throw any exceptions or anything

play13:23

so now with this method call we have an

play13:25

optional that contains the age of that

play13:28

cat and to safely get the value of that

play13:31

optional we can just use a method like

play13:34

or else which if you remember we'll just

play13:36

return the value inside the optional if

play13:39

it exists and otherwise it will return

play13:41

some default that we give it here it

play13:43

probably makes sense to return a default

play13:46

age of zero so now if our original cat

play13:50

optional has a value and its age has a

play13:52

value then this will successfully return

play13:55

that age but if either this original cat

play13:58

optional was empty or its age was empty

play14:01

it will just return a default age of

play14:03

zero and it does all of that logic with

play14:06

just this tiny bit of code of course you

play14:08

can have even more layers of

play14:10

transformation in here as well and once

play14:12

you get used to using it it can be

play14:13

really powerful and there are other

play14:15

methods that are offered by optionals as

play14:17

well and i encourage you to take some

play14:19

time and go through them and play around

play14:21

with them so now with all of that said a

play14:23

question you might have is so where

play14:26

should i be using optionals should i use

play14:28

them anywhere something might be null

play14:31

what exactly should i do and here's the

play14:33

answer that not everybody wants to hear

play14:35

optionals are really intended to only be

play14:38

used

play14:39

how we're using them here and that's as

play14:42

a return type where without optionals

play14:45

your method has the possibility of

play14:47

returning null so it is certainly

play14:49

technically possible to have an optional

play14:53

as like a method parameter optionals are

play14:55

just like any other objects so

play14:57

technically you can create and use them

play15:00

wherever you want but the truth is that

play15:02

optionals were only intended to be used

play15:04

as a return type and that's to blatantly

play15:07

tell the user of that method that the

play15:09

value that they're looking for might not

play15:12

exist and they have to deal with that

play15:13

situation they're not intended to be

play15:15

used just anywhere you have a null

play15:18

variable it's meant for just this type

play15:20

of situation so try not to overuse them

play15:23

but in this type of situation they can

play15:25

add a lot of value and help you avoid

play15:28

null pointer exceptions as always if you

play15:30

enjoyed this video or learned something

play15:32

please let me know by hitting the like

play15:34

button and be sure to subscribe so you

play15:36

don't miss each new java tutorial and be

play15:38

sure to check out my full java course in

play15:40

the link down in the description as

play15:41

always thank you so much for watching i

play15:43

really do appreciate you being here with

play15:45

me

play15:46

and i'll see you next time

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Java ProgrammingOptionals TutorialNull SafetyError HandlingSoftware DevelopmentCoding Best PracticesErgonomic OfficeFlexispot DeskStanding DeskProductivity Tools
Besoin d'un résumé en anglais ?