— Scott McNealy, Sun Microsystems Chairman, President and ceo


C B oo k  o bje ct B oo k  o bje ct Book b



Download 40,57 Mb.
Pdf ko'rish
bet79/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   75   76   77   78   79   80   81   82   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

C
B
oo

o
bje
ct
B
oo

o
bje
ct
Book
b
C
B
oo

o
bje
ct
Bo
o

o
b
je
ct 
Book
d
Book
c = b;
Assign the value of variable b to 
variable c. By now you know what 
this means. The bits inside variable 
b are copied, and that new copy is 
stuffed into variable c.
Both b and c refer to the 
same object. 
References: 3
Objects: 2
Book
b
C
Boo
k
ob
jec

Bo
o

o
b
je
ct 
d

Life on the garbage-collectible heap
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
1
1
2
2
1
2


58
 
chapter 3
Book
b
Book b = new Book();
Book c = new Book();
b = c;
Declare two Book reference variables. 
Create two new Book objects. Assign 
the Book objects to the reference 
variables.
The two book objects are now living 
on the heap. 
Active References: 2
Reachable Objects: 2
Assign the value of variable c to variable b.
The bits inside variable c are copied, and 
that new copy is stuffed into variable b
Both variables hold identical values.
Both b and c refer to the same 
object. Object 1 is abandoned 
and eligible for Garbage Collec-
tion (GC).
Active References: 2
Reachable Objects: 1
Abandoned Objects: 1
The first object that b referenced, Object 1, 
has no more references. It’s unreachable.
Book
C
B
oo

o
bje
ct
B
oo

o
bje
ct
Book
b
C
Bo
o

o
b
je
ct 
Book
c = null;
Assign the value 
null
to variable c.
This makes c a null reference, meaning 
it doesn’t refer to anything. But it’s still 
a reference variable, and another Book 
object can still be assigned to it.
Object 2 still has an active 
reference (
b), and as long 
as it does, the object is not 
eligible for GC.
Active References: 1
null References: 1
Reachable Objects: 1
Abandoned Objects: 1
Life and death on the heap
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
ga
rb
ag

co
lle
ct
ib
le
h
ea
p
objects on the 
heap
 
This guy is toast.
garbage-collector bait.
C
Bo
o

o
b
je
ct 
Book
Still toast
Book
b

null reference
(not programmed to anything)
Boo
k objec
t
Boo
k objec
t
1
1
2
2
2
1
Not yet toast 
(safe as long as b 
refers to it)


you are here
4
primitives 
and 
references
59
Arrays are objects too 
The Java standard library includes 
lots of sophisticated data structures 
including maps, trees, and sets 
(see Appendix B), but arrays are 
great when you just want a quick
ordered, efficient list of things. 
Arrays give you fast random 
access by letting you use an index 
position to get to any element in 
the array. 
Every element in an array is just 
a variable. In other words, one of 
the eight primitive variable types 
(think: Large Furry Dog) or a 
reference variable. Anything you 
would put in a variable of that type 
can be assigned to an 
array element 
of that type. So in an array of type 
int (int[]), each element can hold 
an int. In a Dog array (Dog[]) each 
element can hold... a Dog? No, 
remember that a reference variable 
just holds a reference (a remote 
control), not the object itself. So 
in a Dog array, each element can 
hold a remote control to a Dog. Of 
course, we still have to make the 
Dog objects... and you’ll see all that 
on the next page.
Be sure to notice one key thing 
in the picture above – the array is 
an object, even though it’s an array of 
primitives. 
Arrays are always objects, whether 
they’re declared to hold primitives 
or object references. But you can 
have an array object that’s declared 
to hold primitive values. In other 
words, the array object can have 
elements which are primitives, but 
the array itself is never a primitive. 
Regardless of what the array holds, 
the array itself is always an object!
int array object (int[])
int[]

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   75   76   77   78   79   80   81   82   ...   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