Serialization and Deserialization Bullet points from Head First Java, Ch. 14 - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Serialization and Deserialization Bullet points from Head First Java, Ch. 14

Description:

Serialization and Deserialization Bullet points from Head First Java, Ch. 14 saving an object between program runs you can save an object s state by serializing the ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 7
Provided by: PatG168
Category:

less

Transcript and Presenter's Notes

Title: Serialization and Deserialization Bullet points from Head First Java, Ch. 14


1
Serialization and DeserializationBullet points
from Head First Java, Ch. 14
2
saving an object between program runs
  • you can save an objects 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

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

4
the object graph
  • when an object is serialized, its entire object
    graph is serialized
  • including any objects referenced by the
    serialized objects 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

5
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 doesnt make sense to make static variables
    part of a particular objects state, since all
    objects of that type (made from that class) share
    only a single static value

6
the end of this PowerPoint file
Hooray!
Write a Comment
User Comments (0)
About PowerShow.com