Download presentation
Presentation is loading. Please wait.
Published byGustave Bordeleau Modified over 6 years ago
1
Serialization and Deserialization Bullet points from Head First Java, Ch. 14
6-Dec-18 serialization.ppt
2
saving an object between program runs
you can save an object’s state by serializing the object to serialize an object, you need an ObjectOutputStream (from the java.io package) streams are either connection or chain streams connection streams can represent a connection to a source or destination typically a file, network socket connection, or the console chain streams cannot connect to a source or destination they must be chained to a connection (or other) stream to serialize an object to a file, make a FileOutputStream and chain it into an ObjectOutputStream to serialize an object, call writeOjbect(theObject) on the ObjectOutputStream you do not need to call any methods on the FileOutputStream 6-Dec-18 serialization.ppt
3
Serializable interface
by default, objects are not serializable to be serialized, an object must implement the Serializable interface Serializable is an empty interface it has no methods that must be implemented its presence is a signal to the runtime that this object can be serialized if a superclass of the class implements Serializable, the subclass can also be serialized even without explicitly declaring implements Serializable 6-Dec-18 serialization.ppt
4
when an object is serialized, its entire object graph is serialized
the object graph when an object is serialized, its entire object graph is serialized including any objects referenced by the serialized object’s instance variables plus any objects those objects reference, and so on if any object in the graph is not serializable, an exception will be thrown at runtime unless the instance variable referring to the un-serializable object is skipped the transient keyword can be used to do mark an instance variable to be skipped an instance variable marked as transient will be restored as null upon deserialization or the default value for a primitive 6-Dec-18 serialization.ppt
5
the return type of readObject() is Object
deserialization using readObject(), read objects in the order in which they were originally written the return type of readObject() is Object deserialized objects must be cast to their original type static variables are not serializable it doesn’t make sense to make static variables part of a particular object’s state, since all objects of that type (made from that class) share only a single static value 6-Dec-18 serialization.ppt
6
the end of this PowerPoint file
Hooray! 6-Dec-18 serialization.ppt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.