Not let's look at another interesting topic called civilization NDC realization at the end of the lesson 2
we would also do a short demo of what we learned. 3
So what we have seen how we can read and write data such as images as well as text but sometimes we 4
also come across situations where we need to write wriggler Djala objects to disk and then at a later 5
time read them back from the disk with the same state. 6
So we're talking about saving objects to disk. 7
Typically you would say objects declare a database. 8
But sometimes as we will soon see you would want to save the entire Java object along with it to the 9
disk know the source and the destination need not be a disk. 10
It can also be something like Malmedy serialization is nothing but the process of writing Djala objects 11
to some destination that is Java objects get saved us byte streams. 12
He had a job object A named eight Izard and to some destination let's say it is their desk. 13
So in this case we said that object has been serialized now at some point in the future we may want 14
to reconstruct object it from its serialized version. 15
And this is done through the process of DCD allegation. 16
So DC deliberation we reconstruct Dala objects back from there byte strings. 17
So the reconstructed Djala object will have the same state that it had when it was serialized that is 18
. 19
Instance variables but have the values as before. 20
Not this DC realization can happen on the same JVM or even on a remote JVM. 21
So as objects can also be transmitted across JVM is not the same deal as an object. 22
We need to first make sure that X class implements this interface class Serializable which is a marker 23
interface from the Java dog IO package and do the actual serialization NDC delegation. 24
We make use of these classes object output stream an object input stream object output stream is 4C 25
realization while object input stream is for D.S. realization and these classes implement the interfaces 26
object output an object input respectably. 27
And here is an example of serialization. 28
We must create an instance of Object or put stream bitches and Chambery a buffer stream that is Intan 29
chain Guia file or putzed in which does the actual writing off bytes on here the objects are written 30
with this file called s object but s e r so the filename can be anything. 31
It need not be object not as here but the extension ACR is normally used as a convention but you can 32
use a different extension. 33
All you can even altogether omit the extension to not buffer output stream is also not required. 34
You would use it only for performance reasons. 35
So you can directly chain object or put stream through the file output stream not to write an object 36
when the method or write object on the object output stream instance here one string object and one 37
date object are being returned to the file object. 38
S E R. 39
Now what string as well as did Garces doing and does Serializable interface. 40
Otherwise these objects cannot be serialized. 41
And here are the steps for ABC television. 42
It's just the opposite. 43
We just use input strings here an object input stream or buffered input stream because and don't change 44
to the file and pushed him which is reading from the file object. 45
As you do read an object from the object stream read object that is used. 46
No the important thing here is that Vinita ensured that we are reading objects in the same order in 47
which they have been returned to the desk. 48
So the first object right will be the string object and the second one would be need also read object 49
motard written an object instance. 50
So we need a dongas appropriately so as you can see here in the first line we are casting a string whereas 51
in the second line we are downcasting it to identify the list. 52
We will also do a short demo of both serialization NBC realization here in this example we are serializing 53
string under it but in the demo we will actually serialize our own custom object that has some state 54
known it took a few use cases of serialization one to alleviate it is useful isn't websites that how 55
high traffic rapid is on these web pages are dynamic web pages that is defined this what pages come 56
from sources like a database model and the content in this for pages is pretty much constant that is 57
you will see the same content for a long period of time. 58
So it's such VEP biggest most of the time did is it from the database but not create into the database 59
. 60
And this date I might have to be right from Mike Dipali tables and that would be an expensive operation 61
. 62
Now if you're familiar with D-W Asus you may understand that it may require joining of multiple tables 63
and the join operation would be very expensive. 64
Mundelein once the data is right you would use it to construct you are Java objects and then pass them 65
to the front end on you are performing these actions for every single piece axis. 66
But if you are performing the expense you do opposite operation along that constructing of the Java 67
object. 68
And as mentioned earlier if the Lapidus have heavy traffic then it would affect the response time of 69
the radius. 70
That is it could drastically skewed on the response times. 71
So in such cases since the content does not change frequently we can construct that Djala object once 72
when the pitches access for the very first time. 73
And then we can serialize a Java object subsequently with every bit axes. 74
The page can be built using the serialized object. 75
This way you are whining the costly database operation and also the object construction operation. 76
You can store the serialized object on the desk or even better cash it in the memory using a tool like 77
memcached memcached is an open source distributed object caching system and it also requires your objects 78
to implement the Serializable interface. 79
If you're interested you can google for that and check it out under his breath. 80
Easy to use and one project that I worked on to do this kind of an issue VCL is Java objects to disk 81
undergirded a huge difference in response time. 82
So the web pages in this case where mostly constant sickener area is when you want to pass Java objects 83
at cross-overs for instance your web service implementation might require Djala objects to be paused 84
as input. 85
In that case you can see that as the Del object on Pasic to the web service which is hosted on that 86
remote server on the web service will end don't BCPL as the serialist Joe object. 87
Another interesting use case seems to be post-mortem analysis which just perform when you run into any 88
serious errors for instance if an object is causing some error then you would serialize that object 89
. 90
Let's say you were booted from the catch block later you can. 91
DC realized it using a debugger and can investigate why the object caused that particular error. 92
I've never done this but I'm including a go for a pointer in the resources section. 93
There one particular user is discussing this use case on Harvard. 94
How would this be. 95
As you have time you can take a look at it not just look at some more things that we need to know about 96
serialization. 97
First is that both primitives as well as are is are both Serializable by default. 98
No this would mean two things. 99
First is that if an object to see more then any Prema do variables as well as any it was that it has 100
will also be saved. 101
Good risk. 102
Second is that you can spasmed directly to the White object Mathare off object or put stream next to 103
serialization saved and object Goffe. 104
That is if an object is serialized than any objects it references or any object doughs objects reference 105
will all be serialized. 106
Now if at least one object and object graph does not implement the Serializable interface then seeing 107
an addition of the original object is also within the object graph of the same object is referenced 108
. 109
Mike Dipali times that is there are multiple references to the same object density tradition is smart 110
enough to save the object only once so it's not going to say multiple times. 111
Know dystopic videos I'm not serializable and that's because serialization is about objects. 112
It will start to really be not class variables on hence on not to any particular object. 113
So during B.C.E. realization is Dodik variable will be assigned the value the class has a particular 114
instance of. 115
Next one as if a superclass is serializable then it's subclasses are automatically Serializable that 116
a subclass us do not have to explicitly implement the Serializable interface superclasses Nazirite risible 117
then some classes can still be mere serializable. 118
So if a third party grows that you want to use is not serializable then you can always suppress it and 119
make the subclass serializable. 120
No you do not want an instance variable to be serialized. 121
Then you can model that with the last transmission. 122
So content is a keyword that you would include in the variables declaration. 123
And with that the variables valuable squibbed during serialization and doing DC idealisation that TRONSON 124
variable gets their default value based on its type. 125
So I an object reference variable it will be null unforgettable like and it would be zero non-literary 126
total eclipse. 127
Googoosh shut them all off. 128
Serialization NDC transition. 129
OK here we are in the demo class and some antiquated package. 130
And here we're going to see the as a Java object. 131
NBC realized that. 132
OK so let's just go ahead and look at the class itself. 133
The class of the object and here it is at the top. 134
It's called the Serializable demo and it is a nested class stocktake nested class which means that it 135
is a class that is nested within the class I or. 136
OK. 137
And we still did not discuss nested classes so that we discuss later. 138
So let's not worry about it. 139
But this Serializable demo class. 140
It could also have been an external It need not. 141
It need not be a strict class. 142
It can also be a separate class by itself. 143
But I just made it an abstract class ok under a static master class. 144
And if you recall from our coding conventions we said that's all stocktake masterclasses should be placed 145
before static methods. 146
So I would a more class has all static methods. 147
We know that. 148
So this particular stocktake master class is placed before the starting mothers. 149
OK so we are following the standard convention. 150
And in order for its objects to be serializable we know that it needs to implement Serializable interface 151
. 152
So that's what we are doing. 153
So it's a marker interface interface on this particular class has a variable name and there is a getter 154
on set name for that. 155
So we are going to set the stage for that. 156
And there is also another variable ID so name is an instance variable ID is also an instance variable 157
but ID is marked as transients which means that it should not be serialized. 158
And it is initialized with the value for. 159
And here there is another variable gender. 160
But let's not worry about it for now so I just call him undernote on which also not worry about this 161
moment with a look at it later. 162
So let's just go back to the main method and here it is. 163
So we are for sequential serializer. 164
And in order to do the serialization we are passing a parameter to the an argument to the I order more 165
glass if the argument Bosnia's true. 166
Only then we pull home the Syrian opposition and otherwise we do not. 167
And there is also in order to perform the serialization we have this method called Boosey realization 168
. 169
And in order to perform B.C.E. tradition we have written this matter Boosey that position do B.C.E. 170
demonization. 171
So let's look at this matter Boosey television. 172
Here it is. 173
So you are first creating an instance off Serializable demat because we want to see Golez that particular 174
object and we are said in the state of mean Kujawa. 175
And we are simply printing the name value and the ID value which is a transient variable. 176
So this is before serialization and we will also print after Signa mission to do the actual serialization 177
. 178
Here we are we are creating an object output stream which is chained to a buffer output stream which 179
is intention to fight or put stream on we are writing the object of this file called City or Daut SDR 180
and we are using that right object method and we are simply passing the Serializable to a more object 181
object reference. 182
OK so we are writing the object here. 183
Now let's look at the DC validation method. 184
Here it is. 185
So here we are using input streams. 186
So object input stream buffered input stream on file input stream on the file and this city Aldora as 187
E.R and we are using the read object method here. 188
So once again we are using a try catch block here. 189
I tried with resources read object but read object against an object an instance of Object. 190
So we need to gaster it here to see it rise above them all and we are ending the values after serialization 191
the state one is the mean on the other is I-T. 192
OK so let's just go ahead and fundus but it's busto value due to the configuration. 193
So it's going to the configuration or an ass 194
here. 195
So Program arguments should be true. 196
It's like an electronic case or printed some output here in Demel. 197
If I refresh it you can see that it created this city or not as E.R So that's byte streams that God 198
created. 199
OK so the duckster file. 200
And here it is. 201
So before in the Boosey deposition method the name is Java ideas for but after the D.C. delegation process 202
the name is once again we are reconstructing the object and we have the value of name as Java. 203
But what ID since it's a transient variable it is initialized with the default value. 204
So if it is a constant variable it's not going to be serialized. 205
We know that. 206
And so during these utilization it will assign the value of its type the default value of its type since 207
it's on and it got assigned zero. 208
OK now let's make a small change. 209
Let's go back to this matter here and let me let's see. 210
So the object has been set. 211
Now let's see. 212
You know using a time machine we went into the future and B how changed. 213
We have ordered this new instance variable called gender. 214
OK. 215
So let's assume that it's a new release. 216
And so we have a better class. 217
But the object is from an older of these. 218
So let's see if it will give us any problems. 219
So let's go into a run configuration let's not beat Eleazer this let's not see the lizard this time 220
. 221
So in our city modus and let's say a polite unmix Ron on and you can see that. 222
So naughty delegation has been done on the DC idolization off the object that we'll see realized earlier 223
is being performed on Nix's invalid class exception. 224
So there is an exception here it says local class incompatible. 225
So what is happening is the class has evolved. 226
But the object that we see realized was an old object. 227
So it's like a legacy object and it should not be that way. 228
So the object should be compatible with that class. 229
OK. 230
So there is a version mismatch. 231
So there is something that we need to still learn about utilization and we will see how we can fix this 232
by making some changes and that we are going to learn in the next lesson or at least some of the problems 233
can be addressed by doing that. 234
But that will be something that we look at the next lesson. 235
Okay. 236
Thank you. 237
And happy coding.