Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet226/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   222   223   224   225   226   227   228   229   ...   341
Bog'liq
Effective Java



ITEM 55: RETURN OPTIONALS JUDICIOUSLY
253
An 
Optional
is an object that has to be allocated and initialized, and reading the
value out of the optional requires an extra indirection. This makes optionals
inappropriate for use in some performance-critical situations. Whether a particular
method falls into this category can only be determined by careful measurement
(Item 67).
Returning an optional that contains a boxed primitive type is prohibitively
expensive compared to returning a primitive type because the optional has two
levels of boxing instead of zero. Therefore, the library designers saw fit to provide
analogues of 
Optional
for the primitive types 
int

long
, and 
double
. These
optional types are 
OptionalInt

OptionalLong
, and 
OptionalDouble
. They
contain most, but not all, of the methods on 
Optional
. Therefore, 
you should
never return an optional of a boxed primitive type,
with the possible exception
of the “minor primitive types,” 
Boolean

Byte

Character

Short
, and 
Float
.
Thus far, we have discussed returning optionals and processing them after they
are returned. We have not discussed other possible uses, and that is because most
other uses of optionals are suspect. For example, you should never use optionals as
map values. If you do, you have two ways of expressing a key’s logical absence
from the map: either the key can be absent from the map, or it can be present and
map to an empty optional. This represents needless complexity with great
potential for confusion and errors. More generally, 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   222   223   224   225   226   227   228   229   ...   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