Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet222/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   218   219   220   221   222   223   224   225   ...   341
Bog'liq
Effective Java



ITEM 55: RETURN OPTIONALS JUDICIOUSLY
249
Item 55: Return optionals judiciously
Prior to Java 8, there were two approaches you could take when writing a method
that was unable to return a value under certain circumstances. Either you could
throw an exception, or you could return 
null
(assuming the return type was an
object reference type). Neither of these approaches is perfect. Exceptions should
be reserved for exceptional conditions (Item 69), and throwing an exception is
expensive because the entire stack trace is captured when an exception is created.
Returning 
null
doesn’t have these shortcomings, but it has its own. If a method
returns 
null
, clients must contain special-case code to deal with the possibility of
a null return, unless the programmer can 
prove
that a null return is impossible. If a
client neglects to check for a null return and stores a null return value away in
some data structure, a 
NullPointerException
may result at some arbitrary time
in the future, at some place in the code that has nothing to do with the problem.
In Java 8, there is a third approach to writing methods that may not be able to
return a value. The 
Optional
class represents an immutable container that can
hold either a single non-null 
T
reference or nothing at all. An optional that
contains nothing is said to be 
empty
. A value is said to be 
present
in an optional
that is not empty. An optional is essentially an immutable collection that can hold
at most one element. 
Optional
does not implement 
Collection
, but it
could in principle.
A method that conceptually returns a 
T
but may be unable to do so under
certain circumstances can instead be declared to return an 
Optional
. This
allows the method to return an empty result to indicate that it couldn’t return a
valid result. An 
Optional
-returning method is more flexible and easier to use than
one that throws an exception, and it is less error-prone than one that returns 
null
.
In Item 30, we showed this method to calculate the maximum value in a
collection, according to its elements’ natural order.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   218   219   220   221   222   223   224   225   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish