— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet228/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   224   225   226   227   228   229   230   231   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

236
 
chapter 9
The Stack and the Heap: where things live
Before we can understand what really happens when 
you create an object, we have to step back a bit. We 
need to learn more about where everything lives 
(and for how long) in Java. That means we need to 
learn more about the Stack and the Heap. In Java, we 
(programmers) care about two areas of memory—the 
one where objects live (the heap), and the one 
where method invocations and local variables live 
(the stack). When a JVM starts up, it gets a chunk of 
memory from the underlying OS, and uses it to run 
your Java program. How much memory, and whether 
or not you can tweak it, is dependent on which 
version of the JVM (and on which platform) you’re 
running. But usually you won’t have anything to say 
about it. And with good programming, you probably 
won’t care (more on that a little later).
We know that all objects live on the garbage-collectible 
heap, but we haven’t yet looked at where variables 
live. And where a variable lives depends on what kind 
of variable it is. And by “kind”, we don’t mean 
type 
(i.e. primitive or object reference). The two kinds of 
variables whose lives we care about now are instance 
variables and local variables. Local variables are also 
known as stack variables, which is a big clue for where 
they live.
The Stack
Where method invocations 
and local variables live
main()
doStuff()
go()
Button objec

Duck object
Sno
wboard ob
je
ct
The Heap 
also known as 
“The Garbage-
Collectible Heap”
Where ALL objects live
Instance Variables
Local Variables
Instance variables are declared inside a class but not 
inside a method. They represent the “fields” that each 
individual object has (which can be filled with different 
values for each instance of the class). Instance variables 
live inside the object they belong to.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   224   225   226   227   228   229   230   231   ...   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