Programming 2 Flashcards
Serialization
The flattening of an object structure to allow for storage in a file
Deserialization
The reading of an object stored in a file to allow for it to be used
Where does the relative path start from?
The project folder
Input and Output Streams
An abstract class that holds a stream of bytes. (Often representing a file, can be some other I/O)
How to add bytes to an OutputStream
write(int integer) or write(byte[] array)
Reader and Writer
An abstract class that holds a stream of characters
FileWriter/FileOutputStream constructor
Class(file, ifModifyingFile) (if not modifying will wipe existing data).
FileInputStream read
stream.read(array) reads all the bytes from the stream to a given array
FileReader read
reader.read() - reads one character, or -1 if end of stream
ByteArrayStream
Does reading and writing directly into memory, ususally used for generating temporary data
Pipeline Stream
Used for communication between two threads
Print Stream
Output only. Allows for outputting data in text form. Is a wrapper of an output stream
Tagging Interface
Has no methods
Serializing Requirement
The class must implement the Serializable tagging interface. All fields primitives/String/Serializable
What serializes/deserializes objects
ObjectStreams
Serializing/desializing steps
Create an output/input stream to place/recieve data. Pass this to a objectStream. Call write/readObject on the object stream passing the object to serialize if writing
Serial Version UID
serialisation
64 bit secure hash of the class saved with a serialized object. When deserializing can be compared to current class and if it does not match serialization fails. May still work with minor changes
Externalizable
Interface that allows for choosing of what to serialize. Contains writeExternal and readExternal. Requires an empty constructor
Default Deserialization
From the class type from the stream, go the the highest serializable superclass and call the no argument constructor of its superclass.This sets up initial variables and the object. Work down the heirarchy creating and assigning additional varibles from the stream, until the final class is reached
transient
Keyword for variables that prevents it from being serialized
Ways to make a String
- Direct assignment
- String constructor with array of bytes/chars
- String constructor with a String
compareTo
method in the String class that compares the unicode representation (character by character, so gives which is alphabetically first) of the String and the String parameter. output = First String - parameter string
Anonymous Object
Created object with no name, so will be deleted after used
Immutable
Contents of object cannot be changed. Property of a String.
String Creation Heap Usage
When using direct assignment all Strings with that same value will be stored in the same heap location. When using the String constructor it will create it’s own unique heap location
valueOf
static method converts primitives to new String objects
trim
String method that removes leading and trailing whitespaces
JVM
Abstract description of an architecture (virtual hardware, memory layout, registers, …) for implementing Java
Class Loader
JVM
Locates .class files. Verifies that they are well-formed classes. Loads them into Method Area, links and initialises class (static) variables
JVM Shared Memory
Contains 1 heap and 1 Method Area
JVM Non-shared Memory
Each thread has a Java Stack and Program Counter (no registers)
Java Stack
JVM
Holds state information for thread (e.g. called methods, storing local variables, parameters and doing calculations)
Execution Engine
JVM
Describes how bytecode is executed, and what should happen, in terms of the JVM instruction set. Separate instances for each thread
Method Area
JVM
Stores class information (e.g. name, modifier(public…), field information), method definitions and class (static) variables. Stores a constant pool for each type of Class.
Method Area Structure
JVM
Often use method tables, each class has a table of pointers to methods that can be called on it, including from superclasses
Constant Pool
JVM
Contains ordered list of constants used by the class, i.e. literals (strings, integers) and symbolic references (to methods(ones called by the class methods), classes). If the symbolic reference refers to another loaded class then the reference will be replaced with a pointer to it (constant pool resolution)
Heap
JVM
Stores object, along with their (non-static) variables, including inherited ones. Each object has a pointer to it’s class in the method area
Non-Shared Memory
JVM
Contains a program counter and a stack for each thread
JVM Program Counter
Points to instruction being executed in Method Area.
JVM Stack
Allows for only push/pop frame. When a thread calls a method, frame (containing state information on thread execution) is pushed onto stack. All local variables, parameters and calculations done on frame. Can call other method, which puts the other method on top on stack
Stack Frame local Variable (and parameters) Storage
JVM
Stored as array of words. These variables are referenced by index. Will store primitives directly and references to objects in heap. The zero index always references the object that the method is called on (when non-static method)
Operand Stack
JVM Stack Frame
Storing arguments for any instructions. e.g. in a = 1, 1 would first be stored in the operand stack, then put into the variables array
Stack Frame Pointers
JVM
- To its constant pool
- To the code to go back to after it is done
- To the exception table
Exception Table
JVM
Holds a method’s catch clauses. Matching catch clause searched for when exception happens. If found, PC updated to catching code. Else, method terminated and exception rethrown to previous frame
Tail Recursion
JVM
If the last line of code is the recursive call then the same stack frame could be used. Java does NOT do this
Checked Exceptions
Exceptions that the compiler forces you to catch or defer
Unchecked Exceptions
Don’t need to catch/defer exception as unchecked exceptions should not happen if the code is written properly. Includes runtime exceptions
Error
subclass of throwable indicating serious problem, unchecked
Finally
In try catch block
Final clause, always run
Static Variables
The variable is now for the class, all objects of that class share the same value. Can be changed with Class.variable or object.variable.
Static Methods
Invoked in the class, not the object. Can only use static variables and methods
Final Variables
Can only be changed once when initialised if non-static, or only when declared if static
Extending Thread Class
to do threading
Overwrite the run method and call object.start.
Implementing Runnable Interface
to do threading
Write run method and then objects can be used in thread constructor. The thread can the be started.
Benefits of Runnable
for threading
Allows for a different class to be extended. Data is shared more easily as can call start on same thread multiple times
sleep
thread method
Thread sleeps for given milliseconds
thread priorities
Can use getter and setters to set priority, from 1-10
yield
thread method
Skips next slot of execution