User:Muffingg/Java02

The following are the week 2 notes of the Java Course

Collection (INTERVIEW QUESTION)

 * A way of grouping objects of commonality together (e.g. int and int)
 * cannot take in boolean or char (only object types)
 * A more advanced form of an array (generally mutable in contents and size, i.e. don't need to specify size)
 * Things you can do in collections:
 * Sort
 * Search
 * Order
 * Each entry within a Collection is called an element

Ordered
+ Faster for searching purposes
 * An ordered collection means you can iterate through the collection in a specific order
 * They know something about the previous and next element (index 0, 1, 2,...)
 * An unordered collection has no links with any other elements
 * You can still iterate through them but they will not be in an order

Sorted

 * Ordered collections can be sorted or unsorted
 * unordered is always unsorted as there is no order
 * Sorted has an order according to a rule
 * Unsorted has an order that is imposed by a collection with no relation with the elements

Collection Hierarchy

 * Collection (Think SQL)
 * List
 * Queue
 * Set


 * Map is not part of the Collection Hierarchy

Lists
 * Allows duplicates
 * Also known as a sequence
 * Index Driven
 * Starts with zero
 * Each element has an index describing its location

* List ** LinkedList ** ArrayList ** Vector (same as an ArrayList but thread-safe and slower, synchronised)

LinkedList vs ArrayList

 * LinkedList is faster for adding and removing elements
 * Each element has information on next and previous elements
 * ArrayList performs better when accessing a specific element
 * Removing an element will shift everything 'one step backwards'

Queue

 * Allows duplicates
 * FIFO - First in First out
 * LIFO - Last in First out
 * Different access methods
 * one throws an exception if nothing found
 * one returns a special value if nothing found


 * Queue (i)
 * LinkedList (useful for adding elements at the end, also inherits list and deque)
 * PriorityQueue (orders elements by priority)

Set

 * Doesn't allow duplicates
 * uses .equals method to check if it exists already
 * can only contain one null max
 * by default it is unordered


 * Set (i)
 * SortedSet (i)
 * TreeSet (ordered on sorted order)
 * HashSet (fast access, no ordering)
 * LinkedHashSet (ordered on insertion order)

Map

 * each value has a key (key value pairs)
 * can't have duplicate keys
 * not a descendant of Collection (not in Collection Hierarchy)
 * can be useful for online shopping baskets

Map Hierarchy
 * Map (i)
 * HashMap (fastest update, one null key)
 * LinkedHashMap (ordered on insertion)
 * Hashtable (slower than HashMap, no nulls, thread safe)
 * SortedMap (i)
 * TreeMap (sorted map)

Comparable & Comparative

 * Both are interfaces
 * Needed for sorting things

Comparable

 * compares itself to another thing
 * takes one argument

public int compareTo(object o);


 * returns an int
 * 0 if they are the same
 * 1 if it is greater than object o
 * -1 if is less than object o

Comparative

 * takes two different things and compares them to one another
 * takes two arguments

public int compare(Type t1, Type t2);

Overriding Hashcode and Equals

 * These need to be overridden (usually in collections) to compare contents of different objects
 * Otherwise it would use the == method
 * String overrides the .equals

Generics

 * Old code of making Lists:

List list2 = new ArrayList;


 * New method:

List list1 = newArrayList;


 * Raw Type

List list = new List;

public static  void test(T arg){ } public static void main(String[] args) { VarianceTest.test("Hello"); } public static  Collection nameofmethod(T[] array) { }
 * Enables you to specify the type of object that another object should deal with in a dynamic way
 * Typically used for storage and collections
 * Can allow you to specify specific interfaces / abstract classes to work from
 * Type Erasure - the list object is only stored at compile time
 * The generic type is added in the method as follows:
 * When calling the method:
 * To make T specific (e.g. a number):

 Autoboxing
 * The system automatically converts the primitive (int) to an object type (Integer)
 * Opposite is unboxing

Java IO

 * Stream is temporary storing data (a flow of data)
 * IO is not simply Input/Output. It's Input Process Output
 * Input --> Process --> Output (Will come up in the Friday Exam)
 * An input is a source of data, a process can read
 * Output is the destination a process will write to

Input Stream / Output Stream

 * based on bytes (takes bytes in and passes them to the processor)
 * void close(c) - to close the input stream
 * read(byte[] b) - can take a single or multiple bytes
 * returns an int
 * returns -1 for end of file (hence it returns an int)

new FileInputStream(File file) new FileInputStream(String name)

InputStream / OutputStream

 * read and write characters
 * void close
 * void write(int b)
 * void write(byte[] b)
 * void flush - forces any writes to complete

Provided streams
These include:
 * System.in
 * System.out
 * System.err

DataInputStream / DataOutputStream

 * read and write primitives except char

Reader/Writer

 * same methods as InputStream / OutputStream except read(byte[] b)

new InputStreamReader( new FileInputStream(...) );

Hierarchy

 * Reader (characters)
 * InputStreamReader
 * FileReader
 * InputStream (bytes)
 * FileInputStream


 * in order to read InputStreamReader you need to read the FileInputStream

new BufferedReader(new File("./test.txt")

Spy

 * When we want to stub a class, we use Spy

Priority Queue
queue.add(new Colour (1,"black")); queue.add(new Colour (3,"blue")); queue.add(new Colour (2,"red"));
 * allows you to prioritise a Queue (e.g. using numbers)


 * The class would have to implement Comparable

Serialisation

 * Marshalling means changing it's state
 * Serialisation is the marshalling of an object's state into a stream of bytes, which can be directed into a file / array of bytes
 * Opposite is to de-serialise (aka unmarshalling)
 * Useful for:
 * Saving / Restoring a game's state
 * Transferring an object over a network
 * Used in clustered architectures load balancing mechanisms need to transfer objects across servers
 * In java an object cannot be serialised by default
 * need to use the java.io interface
 * an interface without methods (aka tag / marker interface)

Inner Classes

 * Used when the code is too small or simple that there is no point for having a separate class for it

Instance Inner Classes

 * Good for information hiding (to access private data from outside)
 * Can be private (invisible from outside the enclosing class) or protected
 * has close relationship to the enclosing class
 * without enclosing class instance, you would not be able to create an instance of the inner class
 * can access private data

Anonymous Inner Classes
{ {
 * most commonly used inner class
 * Unknown, doesn't have a name
 * To use a method, we'll call an interface (or superclass / abstract class) and the inner class would implement the method
 * can only be used with the superclass
 * only methods in the superclass are visible
 * can be created inside the methods as well as outside the methods
 * Override methods from the Superclass
 * Used for ActionListeners
 * The following is known as the initialiser block

Static Inner Classes

 * no relationship with enclosing class
 * can access private data from enclosing class

Local Inner Classes

 * have names
 * can implement classes and interfaces
 * must be inside a method
 * see methods from the superclass and the ones that are not from a superclass
 * can view methods that you create it in the local ones

Documentation

 * Something that is not code that will explain what your code does, why it does it, how it does it

Types of comments

 * Inline comment //
 * Multiline comment /* ..........   */
 * Documentation /** ..........   */
 * Comments should describe what it does, not how it does it
 * They should also not decribe the obvious

JavaDoc

 * The documentation is written above the class signature

/**  * An Object that presents login menu’s * and retrieves user input * @author John Smith */ public class LoginView{ }


 * JavaDoc will ignore the code and only look at the documentation
 * Order of documentation goes as follows:
 * Fields, Constructors, Methods, Inheritances
 * Shift + Alt + J can be used to document a certain method / class
 * JavaDoc is a markup language and looks for certain tags e.g. @Return
 * @Return shows what the return type of that method is

Examples

 * @author - Who created / modified (class/interface/enum)
 * @throws - What exceptions are thrown & why (methods/constructors)
 * @version - version that the class/interface is created
 * @since - use for methods, since when they have been available
 * {@code example} - the word example will be shwon in a slightly different format (looking like a code)
 * @deprecated - when a new method replaces a old method, it will maintain backwards compatibility if the method/class is to be removed in the future