Head First Java a brain-Friendly Guide Kathy Sierra and Bert Bates



Download 17,18 Mb.
Pdf ko'rish
bet48/90
Sana22.06.2022
Hajmi17,18 Mb.
#693704
1   ...   44   45   46   47   48   49   50   51   ...   90
Bog'liq
(Head Up) Kathy Sierra Bert Bates - Head First Java, 3rd Edition Early Release-O\'Reilly Media, Inc. (2021)

Make an array of Dogs
Declare a Dog array variable
Dog[] pets;
Create a new Dog array with a length of 7, and assign it to the previously-declared 
Dog[]
variable 
pets
pets = new Dog[7];
What’s missing?
Dogs! We have an array of Dog references, but no actual Dog objects!


 Create new Dog objects, and assign them to the array elements.
Remember, elements in a Dog array are just Dog reference variables. We still need Dogs!
pets[0] = new Dog();
pets[1] = new Dog();


 SHARPEN YOUR PENCIL
What is the current value of pets[2]? ___________
What code would make pets[3] refer to one of the two existing Dog objects?
_______________________
JAVA CARES ABOUT TYPE.
Once you’ve declared an array, you can’t put anything in it except things that are of the declared array
type.
For example, you can’t put a Cat into a Dog array (it would be pretty awful if someone thinks that only Dogs
are in the array, so they ask each one to bark, and then to their horror discover there’s a cat lurking.) And you
can’t stick a 
double
into an 
int
array (spillage, remember?). You can, however, put a 
byte
into an 
int
array, because a 
byte
will always fit into an 
int
What happens if the Dog is in a Dog array? -sized cup. This
is known as an 
implicit widening. We’ll get into the details later, for now just remember that the compiler
won’t let you put the wrong thing in an array, based on the array’s declared type.
Control your Dog
(with a reference variable)
Dog fido = new Dog();
fido.name = “Fido”;
We created a Dog object and used the dot operator on the reference variable 

Download 17,18 Mb.

Do'stlaringiz bilan baham:
1   ...   44   45   46   47   48   49   50   51   ...   90




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