Effective Java


probably something wrong with its API



Download 2,19 Mb.
Pdf ko'rish
bet132/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   128   129   130   131   132   133   134   135   ...   341
Bog'liq
Effective Java

probably something wrong with its API.
Prior to Java 8, the type inference rules were not clever enough to handle the
previous code fragment, which requires the compiler to use the contextually spec-
ified return type (or 
target type
)
 
to infer the type of 
E
. The target type of the 
union
invocation shown earlier is 
Set
. If you try to compile the fragment in an
earlier version of Java (with an appropriate replacement for the 
Set.of
factory),
you’ll get a long, convoluted error message like this:
Union.java:14: error: incompatible types
Set numbers = union(integers, doubles);
^
required: Set
found: Set
where INT#1,INT#2 are intersection types:
INT#1 extends Number,Comparable
INT#2 extends Number,Comparable
Luckily there is a way to deal with this sort of error. If the compiler doesn’t
infer the correct type, you can always tell it what type to use with an 
explicit type


ITEM 31: USE BOUNDED WILDCARDS TO INCREASE API FLEXIBILITY
143
argument
[JLS, 15.12]. Even prior to the introduction of target typing in Java 8,
this isn’t something that you had to do often, which is good because explicit type
arguments aren’t very pretty. With the addition of an explicit type argument, as
shown here, the code fragment compiles cleanly in versions prior to Java 8:
// Explicit type parameter - required prior to Java 8
Set numbers = Union
.
union(integers, doubles);
Next let’s turn our attention to the 
max
method in Item 30. Here is the original
declaration:
public static > T max(List list)
Here is a revised declaration that uses wildcard types:
public static

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   128   129   130   131   132   133   134   135   ...   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