— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet498/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   494   495   496   497   498   499   500   501   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

540
 
chapter 16
Generics means more type-safety
We’ll just say it right here—virtually all of the code you write that deals 
with generics will be collection-related code. Although generics can be used 
in other ways, the main point of generics is to let you write type-safe 
collections. In other words, code that makes the compiler stop you 
from putting a Dog into a list of Ducks.
Before generics (which means before Java 5.0), the compiler could 
not care less what you put into a collection, because all collection 
implementations were declared to hold type Object. You could put 
anything in any ArrayList; it was like all ArrayLists were declared as 
ArrayList
With generics, you can 
create type-safe collections 
where more problems are 
caught at compile-time 
instead of runtime.
Without generics, the 
compiler would happily let 
you put a Pumpkin into an 
ArrayList that was supposed 
to hold only Cat objects.
WITHOUT generics
ArrayList
Object
Object
Object
Object
Objects go IN as a reference to 
SoccerBall, Fish, Guitar, and 
Car objects
And come OUT as a reference of type Object
WITH generics
Objects go IN as a reference to 
only Fish objects
And come out as a reference of type Fish
ArrayList
Before generics, there was no 
way to declare the type of an 
ArrayList, so its add() method 
took type Object.
Now with generics, you can put only Fish 
objects in the ArrayList, so the 
objects come out as Fish references. 
You don’t have to worry about someone 
sticking a Volkswagen in there, or that 
what you get out won’t really be cast-
able to a Fish reference.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   494   495   496   497   498   499   500   501   ...   555




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