Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


Question 60: What is Externalizable Interface?



Download 7,31 Mb.
Pdf ko'rish
bet39/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   35   36   37   38   39   40   41   42   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 60: What is Externalizable Interface?
Answer: The default serialization process is very slow as it is fully
recursive, so whenever we try to serialize one object, the
serialization process tries to serialize all the fields of our class
(except static and transient variables). So, if we have a class with
lots of variables present and we do not want to serialize all of them,
we have to make all of those variables as transient, all these fields
will always be assigned with default values. This makes the entire
process very slow.
Externalizable interface is used when we want to implement custom
logic to serialize/deserialize an object. Externalizable interface
extends the Serializable interface, and it has two methods,
writeExternal() and readExternal() which are used for serialization
and de-serialization. This way, we can change the JVM’s default
serialization behavior because while using Externalizable, we decide
what to store in stream.
Program 1:
Employee.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\color2.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\color3.png


TestExternalizable.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\color4.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\color5.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 1\output.png


Now, one thing to remember here is that the public no-arg
constructor gets called before readExternal() method, so we have to
provide this no-arg constructor or else we will get an exception
during run-time.
Comment the public no-arg constructor from Employee.java:
Program 2:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 2\color.png


Now, run TestExternalizable.java, you will get below output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
60\Program 2\error.png


So, if interviewer asks a question that you have a class which has
1000 variables and you want to serialize only 10 specific variables,
your answer should be using an Externalizable interface.
Some points to remember:

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   ...   89




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