Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 24

CSE205 Lecture 24

Object-Oriented Programming and Data Structures
Serialization
(202111)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

October 04, 2021
Tweet

Transcript

  1. jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 24:

    Serialization Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 2 jgs

    Announcement § Quiz 06 is due before our next lecture December 2.
  3. jgs Quiz 05 Trees

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs

    Do the following 1. Insert in a binary tree the following numbers (draw the tree) 100, 50, 150, 125, 124, 75, 76, 74, 73, 130, 131 2. Print the tree in pre-order 3. Print the tree in-order 4. Print the tree in post-order
  5. jgs Serialization

  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6 jgs

    I/O Streams § An I/O Stream represents an input source or an output destination
  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 jgs

    BufferedReader PrintWriter I/O Streams binary FileInputStream FileOutputStream FileReader FileWriter char FileReader FileWriter lines
  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs

    Serialization § Java provides a mechanism, called serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
  9. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs

    I/O Streams BufferedReader PrintWriter binary FileInputStream FileOutputStream FileReader FileWriter char FileReader FileWriter lines ObjectInputStream ObjectOutputStream FileInputStream FileOutputStream objects
  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs

    Serialization § the process is JVM independent –an object can be serialized on one platform and deserialized on an entirely different platform. § Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs

    Step 1
  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs

    Serializable For a class to be serialized, two conditions must be met: § The class must implement the java.io.Serializable interface. § All the fields in the class must be serializable. § If you do not want a field to be serializable, mark is as transient, such as public transient int grade;
  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs

    Step 2
  14. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14 jgs

    Step 3
  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs

    Serialization § The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. § For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException. § Notice that the return value of readObject() is an Object. Thus, casting is needed.
  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs

    Questions
  17. jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez,

    Ph.D. javiergs@asu.edu Fall 2021 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.