— Scott McNealy, Sun Microsystems Chairman, President and ceo


Make an ObjectInputStream



Download 40,57 Mb.
Pdf ko'rish
bet408/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   404   405   406   407   408   409   410   411   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

2
Make an ObjectInputStream
ObjectInputStream lets you read objects
but it can’t directly connect to a file. 
It needs to be chained to a connection 
stream, in this case a FileInputStream.
GameCharacter elf = (GameCharacter) one;
GameCharacter troll = (GameCharacter) two;
GameCharacter magician = (GameCharacter) three;
4
Cast the objects 
Each time you say readObject(), you get the next 
object in the stream. So you’ll read them back in 
the same order in which they were written. You’ll 
get a big fat exception if you try to read more 
objects than you wrote.
The return value of 
readObject() is type Object 
(just like with ArrayList), so 
you have to cast it back to 
the type you know it really is.
serialized
deserialized


442
 
chapter 14
What happens during deserialization?
When an object is deserialized, the JVM attempts to bring 
the object back to life by making a new object on the heap 
that has the same state the serialized object had at the time it 
was serialized. Well, except for the transient variables, which 
come back either null (for object references) or as default 
primitive values.
deserializing 
objects
1
The object is read from the stream.
2
The JVM determines (through info stored with 
the serialized object) the object’s class type.
3
The JVM attempts to fi nd and load the ob-
ject’s class. If the JVM can’t fi nd and/or load 
the class, the JVM throws an exception and 
the deserialization fails.
4
A new object is given space on the heap, but 
the serialized object’s constructor does 
NOT run! Obviously, if the constructor ran, it 
would restore the state of the object back 
to its original ‘new’ state, and that’s not what 
we want. We want the object to be restored 
to the state it had 
when it was serialized, not 
when it was fi rst created.
This step will throw an exception if the JVM 
can’t find or load the class!
Object
ObjectInputStream 
(a chain stream)
is read by
class is found and loaded, saved 
instance variables reassigned 
FileInputStream
(a connection stream)
011010010110111001
object is read as bytes
01101001
01101110
01
File
is chained to



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   404   405   406   407   408   409   410   411   ...   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