Effective Java


new E[DEFAULT_INITIAL_CAPACITY]



Download 2,19 Mb.
Pdf ko'rish
bet121/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   117   118   119   120   121   122   123   124   ...   341
Bog'liq
Effective Java

new E[DEFAULT_INITIAL_CAPACITY]
;
^
As explained in Item 28, you can’t create an array of a non-reifiable type, such
as 
E
. This problem arises every time you write a generic type that is backed by an
array. There are two reasonable ways to solve it. The first solution directly circum-
vents the prohibition on generic array creation: create an array of 
Object
and cast


CHAPTER 5
GENERICS
132
it to the generic array type. Now in place of an error, the compiler will emit a
warning. This usage is legal, but it’s not (in general) typesafe:
Stack.java:8: warning: [unchecked] unchecked cast
found: Object[], required: E[]
elements =
 (E[]) new Object[DEFAULT_INITIAL_CAPACITY]
;
^
The compiler may not be able to prove that your program is typesafe, but you
can. You must convince yourself that the unchecked cast will not compromise the
type safety of the program. The array in question (
elements
) is stored in a private
field and never returned to the client or passed to any other method. The only
elements stored in the array are those passed to the 
push
method, which are of
type 
E
, so the unchecked cast can do no harm.
Once you’ve proved that an unchecked cast is safe, suppress the warning in as
narrow a scope as possible (Item 27). In this case, the constructor contains only the
unchecked array creation, so it’s appropriate to suppress the warning in the entire
constructor. With the addition of an annotation to do this, 
Stack
compiles cleanly,
and you can use it without explicit casts or fear of a 
ClassCastException
:
// The elements array will contain only E instances from push(E).
// This is sufficient to ensure type safety, but the runtime

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   117   118   119   120   121   122   123   124   ...   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