— Scott McNealy, Sun Microsystems Chairman, President and ceo


puzzle:  Five Minute Mystery



Download 40,57 Mb.
Pdf ko'rish
bet259/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   255   256   257   258   259   260   261   262   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

puzzle: 
Five Minute Mystery


constructors and 
gc
you are here
4
269
public class TestLifeSupportSim {
public static void main(String [] args) {
ArrayList aList = new ArrayList();
V2Radiator v2 = new V2Radiator(aList);
V3Radiator v3 = new V3Radiator(aList);
for(int z=0; z<20; z++) {
RetentionBot ret = new RetentionBot(aList);
}
}
}
class SimUnit {
String botType;
SimUnit(String type) {
botType = type;
}
int powerUse() {
if (“Retention”.equals(botType)) {
return 2;
} else {
return 4; 
}
}
}
Tom gave the code a quick look and a small smile creeped across his lips. I think Iʼve 
found the problem Sarah, and I bet I know by what percentage your power usage readings are off 
too!
What did Tom suspect? How could he guess the power readings errors, and what few 
lines of code could you add to help debug this program?
Five-Minute
Mystery
continued...


object 
lifecycle
270
 
chapter 9
public class Honey {
public static void main(String [] args) {
Honey honeyPot = new Honey();
Honey [] ha = {honeyPot, honeyPot, 
honeyPot, honeyPot};
Bees b1 = new Bees();
b1.beeHA = ha;
Bear [] ba = new Bear[5];
for (int x=0; x < 5; x++) {
ba[x] = new Bear();
ba[x].hunny = honeyPot;
}
Kit k = new Kit();
k.kh = honeyPot;
Raccoon r = new Raccoon();
r.rh = honeyPot;
r.k = k;
k = null;
} // end of main
}
Honey
Object
( ends up null )
Popular 
Objects
It probably wasn’t too hard to figure out that the Honey object first referred to by the honeyPot variable is by 
far the most ‘popular’ object in this class. But maybe it was a little trickier to see that all of the variables that 
point from the code to the Honey object refer to the same object! There are a total of 12 active references to 
this object right before the main( ) method completes. The k.kh variable is valid for a while, but k gets nulled 
at the end. Since r.k still refers to the Kit object, r.k.kh (although never explicity declared), refers to the object!
1
2
3
4
5
6
7
8
9
copyGC = null;
gc2 = null; 
newGC = gc3;
gc1 = null; 
newGC = null;
gc4 = null;
gc3 = gc2;
gc1 = gc4; 
gc3 = null; 
No - this line attempts to access a variable 
that is out of scope.
OK - gc2 was the only reference variable 
referring to that object.
No - another out of scope variable.
OK - gc1 had the only reference because 
newGC is out of scope.
No - newGC is out of scope.
No - gc3 is still referring to that object.
No - gc4 is still referring to that object.
OK - Reassigning the only reference to 
that object.
No - gc4 is still referring to that object.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   255   256   257   258   259   260   261   262   ...   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