Effective Java


This is the preferred way to use the



Download 2,19 Mb.
Pdf ko'rish
bet112/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   108   109   110   111   112   113   114   115   ...   341
Bog'liq
Effective Java

This is the preferred way to use the 
instanceof
 operator
with generic types:
// Legitimate use of raw type - instanceof operator
if (o instanceof 
Set
) {
// Raw type
Set
s = 
(Set)
o;
// Wildcard type
...
}


CHAPTER 5
GENERICS
122
Note that once you’ve determined that 
o
is a 
Set
, you must cast it to the wildcard
type 
Set
, not the raw type 
Set
. This is a checked cast, so it will not cause a
compiler warning.
In summary, using raw types can lead to exceptions at runtime, so don’t use
them. They are provided only for compatibility and interoperability with legacy
code that predates the introduction of generics. As a quick review, 
Set
is
a parameterized type representing a set that can contain objects of any type,
Set
is a wildcard type representing a set that can contain only objects of some
unknown type, and 
Set
is a raw type, which opts out of the generic type system.
The first two are safe, and the last is not.
For quick reference, the terms introduced in this item (and a few introduced
later in this chapter) are summarized in the following table:
Term
Example
Item
Parameterized type
List
Item 26
Actual type parameter
String
Item 26
Generic type
List
Items 26, 29
Formal type parameter
E
Item 26
Unbounded wildcard type
List
Item 26
Raw type
List
Item 26
Bounded type parameter

Item 29
Recursive type bound
>
Item 30
Bounded wildcard type
List
Item 31
Generic method
static List asList(E[] a)
Item 30
Type token
String.class
Item 33


ITEM 27: ELIMINATE UNCHECKED WARNINGS
123

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   108   109   110   111   112   113   114   115   ...   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