6 - ELEC2602 Object Oriented & Systems Programming LT...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyrights by Dr. V. Tam 1 ELEC2602 – Object Oriented & Systems Programming LT 6: Object Serialization & Threads in Java [ Recommended reading: Ch 9 ~ 11, The Java Programming Language (3 rd Edition) , Sun MicroSystems Press]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyrights by Dr. V. Tam 2 Overview. . Object Serialization in Java Threads in Java Some Useful Online Materials: Java Tutorial: http://java.sun.com/docs/books/tutorial/ J2SE 1.4.1 API Specification: http://java.sun.com/j2se/1.4.1/docs/api/
Background image of page 2
Copyrights by Dr. V. Tam 3 Object Serialization in Java …. Normally, objects do not “persist” beyond the lifetime of the program that created them; However, persistence is required when we write objects to a file to be later read back in, possibly by a different program. Serialization converts an object and its state to a stream of bytes.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyrights by Dr. V. Tam 4 Object Serialization in Java …. (2) As we can see in many advanced Java applications using the Remote Method Invocation (RMI), object serialization is employed to transparently communicate with objects on remote machines. For an object to be serializable : 1. The object must implement the Serializable interface; 2. The object’s member variables are non-static. – serialization will not affect static variables, they may be different when the object is read back in, or de- serialized
Background image of page 4
Copyrights by Dr. V. Tam 5 To Output Objects… The ObjectOutputStream class implements the ObjectOutput interface (an extension of DataOutput); – has all the methods defined there; in particular the writeObject() method. Constructors: ObjectOutputStream(OutputStream out) throws IOException protected ObjectOutputStream() throws IOException, SecurityException Methods (not all): void writeObject(Object o) throws IOException – can send all the fields of an Object, including private and protected fields; – only fields marked transient or static will not be sent; – thus, for security concerns, a class must explicitly be implementing the Serializable or the Externalizable interfaces, before it can be sent using writeObject() ;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyrights by Dr. V. Tam 6 To Input Objects… We often use the ObjectInputStream class to input/read objects from any input stream or file; Constructors: ObjectInputStream(InputStream in) throws IOException Example methods: Object readObject() throws IOException, ClassNotFoundException void defaultReadObject() throws IOException, ClassNotFoundException
Background image of page 6
Copyrights by Dr. V. Tam
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/05/2008 for the course EEE ELEC2602 taught by Professor Dr.vincenttam during the Winter '08 term at HKU.

Page1 / 22

6 - ELEC2602 Object Oriented & Systems Programming LT...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online