1 / 13

Serializable Objects

Learn about serialization in Java, including how to write objects to files, read objects from files, and handle exceptions. Also, explore bitwise operations and their applications.

isims
Download Presentation

Serializable Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Serializable Objects • Serialization is Java’s way of allowing you to write a sequence of bytes representing an object in your program to a file. • Deserialization is the process of reading a file containing serialized objects and ‘reconstituting’ them. See: https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html • Classes whose objects can be serialized need to: • import java.io.Serializable • implement Serializable • Transientvariables are not written or read • private transient int x; declares an instance variable that will be neither written nor read

  2. Serializable Anomalies • When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case a NotSerializableException will be thrown and will identify the class of the non-serializable object. • It is possible for class A to write an object to a file and a different version of class A attempt to read this object back from the file, which causes an InvalidClassException to be thrown on deserialization. • A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long. • ObjectStreamClass.lookup() can be used to retrieve the serialVersionUID for a class. • Java API strongly recommends that all serializable classes explicitly declare serialVersionUID values to prevent version problems that may occur with the system generated serialVersionUID for a class.

  3. Reading and Writing Objects • Before writing objects to sequential disk files, instantiate:FileOutputStream fos = newFileOutputStream(“pathname”);BufferedOutputStream bos = new BufferedOutputStream(fos);ObjectOutputStream oos = new ObjectOutputStream(bos); • Before reading objects from sequential disk files, instantiate:FileInputStream fis = newFileInputStream(“pathname”);BufferedInputStream bis = new BufferedInputStream(fis);ObjectInputStream ois = new ObjectInputStream(bis); • To Write the object: oos.writeObject( objectVar ); • To Read the object: objectVar = (objectType)ois.readObject(); • To close the streams: ois.close(); or oos.close(); • Catch exceptions such as IOException, FileNotFoundException

  4. Bitwise Operations • Assume: • x is a byte with its eight bits set to 00010101 • y is a byte with its eight bits set to 11110011 • Least significant bit (LSB) is the bit farthest to the right • Then • And: x & y results in 00010001 (a bit is set [has value 1] if both of the bits in the same position are set – LSB of x is 1, LSB of y is 1, so LSB of result is 1) • Or: x | y results in 11110111 (a bit is set if either of the bits in the same position are set) • Xor: x ^ y results in 11100110 (a bit is set if either but not both of the bits in the same position are set) • Not: ~x results in 11101010 (change zeroes to ones and ones to zeroes) • Shl: x <<3 results in 10101000 (bits in x shift left three positions) • Shr: y >>> 3 results in 00011110 (bits in y shift right three positions) • Sar: y >> 3 results in 11111110 (bits in y shift right with sign extension -- the value of the most significant bit (MSB) (1) repeats) • Sar: x >> 3 results in 00000010 (bits in x shift right with sign extension -- the value of the MSB (0) repeats)

  5. Why bits? • Embedded controllers and device drivers need to communicate with devices using bitwise operations • Significant storage savings are possible with some applications • A human DNA sequence has billions of letters, where each letter is one of A, C, T, or G. Since there are only 4 values, each letter in the sequence can be stored using only two bits. A byte for each character would require eight times the storage. • Multimedia applications store data in bits. • Sometimes bit operations are faster than using floating point multiplication and division • Databases and operating systems rely heavily on bit manipulation • Encryption algorithms use bit operations Note:Bit operations only on primitive integer values: int, short, character, long

  6. Converting binary to decimal • Assume: • x is a byte with its eight bits set to 01010101 • y is a byte with its eight bits set to 00101011 • What displays? • System.out.println(x) displays 85 (26 + 24 + 22 + 20 = 64 + 16 + 4 + 1) • System.out.println(y) displays 43 (25 + 23 + 21 + 20 = 32 + 8 + 2 + 1) • Remember the carry with binary addition • 1 + 1 is 0 with a carry of 1 to the next digit 01010101 + 00000001 = 10000000 -- -128 displays in the demo – see Twos Complement See demos/BitDemo.java To see hex values in the debugger: Window->Preferences: For primitives, browse to Java->Debug->Primitive Display Options

  7. Converting Decimal to Binary • Powers of 2: 26 (64) 25 (32) 24 (16) 23 (8) 22 (4) 21 (2) 20 (1) • In 49: Need a 64? -- no 0, 32? – yes 1, 16 yes 1 (now am upto 48), 8 no 0, 4 no 0, 2 no 0, 1 yes (48+1 == 49) 1 0110001

  8. Twos complement representation Non-negative numbers - MSB == 0 Negative numbers – MSB == 1 To compute the twos complement of an integer: Negate (Not) all bits then add one. Find Twos complement of 11110011: • Complementing 11110011 gives 00001100. • Add one to 00001100 gives a twos complement value of 00001101 • In decimal, 00001101 is 23 + 22 + 20 = 8 + 4 + 1 = 13. • Because the number was negative, the original value was -13. Shortcut: flip the digits to the left of the least significant one bit. 0000110 1 (13) – flip bits to the left of least significant 1 bit -> 1111001 1 (-13) 111100 10 (-14) – flip bits to left of least significant 1 bit -> 000011 10 (14)

  9. Operations on Bits • We can store YYYYMMDDHHmm in 32 bits (type int) • Fields • mm (6 bits – bits 0 - 5) -- need 60 values • HH (5 bits – bits 6 - 10) – need 24 values • DD (5 bits – bits 11 - 15) – need 31 values • MM (4 bits – bits 16 - 19) -- need 12 values • YYYY (11 bits – bits 20 - 30) – 2047; values • Valid (1 bit – 31) – flag indicating this is a real date -- why? • Bit Mask • an integer with ones in field positions and zeroes elsewhere • int hourM = 31<<6 is the mask for HH • int yyyyM = 4095<<20 is the mask for YYYY • Questions • What are the biggest numbers that can fit in each of the above fields? • In the lab you use longs: • 3L<<35 is the mask for a 2 bit field starting at bit 35 of a long • Why is the 'L' needed in the expression 3L<<35?

  10. Bitwise operations using masks Assume that 'int date = 0;' used to create the variable to hold one of these ‘dates’ • Set the year to 2007 • date &= ~(2047<<21); // clear the year field • date |= (2007<<21); // set the new values • Clear the hour field • date &= ~(31<<7); • Toggle a bit (value ^= 1<<5; toggles bit 5) – equivalent to: value = value ^ (1<<5); • Multiply using bits • x << 3 multiplies by 23 = 8. • x + x<<1 + x<<2 multiplies by 1 + 21 + 22 = 7 • Divide using bits • y>>2 divides by 4, handling positive and negative numbers • y>>>2 divides by 4, but only handles positive numbers

  11. Quality Assurance • Quality assurance • Design review (walkthroughs), unit tests, integration, test suites, alpha-testing, beta-testing • Unit tests • Black-box testing (tests the interface of this unit by assuming no knowledge of how it is implemented) • White/Glass-box testing (force every path to be taken. Must know how the unit is implemented to test it) • Assertions (statements in the code itself that check data values at the point the statement executes) • Debugging Techniques • Breakpoints, watch variables, single stepping • Tracing (inspecting the call stack)

  12. Agile Development Methodology See http://agilemodeling.com/ • Storyboard with users – small amounts of functionality in each release • Write tests (Test driven design) • Develop code • Release code when tests pass • Repeat above steps for next set of functionality. • Continuous integration shops produce daily releases and constantly test with automated test suites.

  13. Case Study • Automated testing programs are in high demand. They allow tests to be taken at times convenient to the individual. • Some issues to consider: • Guarantee that the correct person is taking the test. • Dynamic group of questions • Where should the test be taken • Types and numbers of questions • Test bank • Availability of results • How general purpose • Throwing out of bad questions • Artificial intelligence • What steps do we need for a sound design? Lab 3: Create prototype to demonstrate automated true/false tests

More Related