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