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

CSE205 Lecture 25

CSE205 Lecture 25

Object-Oriented Programming and Data Structures
Serialization and Final Review
(202204)

Javier Gonzalez-Sanchez

October 05, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 205
    Object-Oriented Programming and
    Data Structures
    Lecture 25: Serialization
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View full-size slide

  2. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 2
    jgs
    Exceptions
    There are many exception types available in Java:
    § ArithmeticException,
    § FileNotFoundException,
    § ArrayIndexOutOfBoundsException,
    § SecurityException,
    § etc.
    § The parent of all is the class Exception

    View full-size slide

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

    View full-size slide

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5
    jgs
    finally Statement
    § The finally statement lets you execute code, after try...catch,
    regardless of the result

    View full-size slide

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

    View full-size slide

  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8
    jgs
    Definition
    § The throw statement allows you to create a custom error

    View full-size slide

  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9
    jgs
    Example

    View full-size slide

  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10
    jgs
    Example

    View full-size slide

  9. jgs
    Serialization

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14
    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.

    View full-size slide

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

    View full-size slide

  14. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16
    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.

    View full-size slide

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

    View full-size slide

  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18
    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;

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21
    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.

    View full-size slide

  20. jgs
    Topics to Review

    View full-size slide

  21. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 23
    jgs
    Topics per Week

    View full-size slide

  22. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 24
    jgs

    View full-size slide

  23. jgs
    The following slides shows some examples
    related to some topics
    This is NOT a comprehensive list of topics
    Topics in the exam can be found
    Weeks 2 to 16
    (Lectures 1 to 24)

    View full-size slide

  24. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 26
    jgs
    Questions

    View full-size slide

  25. jgs
    CSE 205 Object-Oriented Programming and Data Structures
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    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.

    View full-size slide