Effective Java


Item 27: Eliminate unchecked warnings



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

Item 27: Eliminate unchecked warnings
When you program with generics, you will see many compiler warnings:
unchecked cast warnings, unchecked method invocation warnings, unchecked
parameterized vararg type warnings, and unchecked conversion warnings. The
more experience you acquire with generics, the fewer warnings you’ll get, but
don’t expect newly written code to compile cleanly.
Many unchecked warnings are easy to eliminate. For example, suppose you
accidentally write this declaration:
Set exaltation = new HashSet();
The compiler will gently remind you what you did wrong:
Venery.java:4: warning: [unchecked] unchecked conversion
Set exaltation = new HashSet();
^
required: Set
found: HashSet
You can then make the indicated correction, causing the warning to disappear.
Note that you don’t actually have to specify the type parameter, merely to indicate
that it’s present with the 
diamond operator
(
<>
), introduced in Java 7. The com-
piler will then 
infer
the correct actual type parameter (in this case, 
Lark
):
Set exaltation = new HashSet
<>
();
Some warnings will be 
much
more difficult to eliminate. This chapter is filled
with examples of such warnings. When you get warnings that require some
thought, persevere!
 Eliminate every unchecked warning that you can. 
If you
eliminate all warnings, you are assured that your code is typesafe, which is a very
good thing. It means that you won’t get a 
ClassCastException
at runtime, and it
increases your confidence that your program will behave as you intended.
If you can’t eliminate a warning, but you can prove that the code that
provoked the warning is typesafe, then (and only then) suppress the warning

Download 2,19 Mb.

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