Česky
Kamil Dudka

Study

File detail

Name:Downloadnotes.java [Download]
Location: study > GJA
Size:7.7 KB
Last modification:2008-04-15 10:35

Source code

@BOOK{AGH,
      author = {Ken Arnold and James Gosling and David Holmes},
      title = {The Java Programming Language},
      edition = {Fourth Edition},
      publisher = {Addison-Wesley},
      year = {2005},
      isbn = {0321349806},
}
===============================================================
- generic types
    interface Lookup<T> {
        T find(String name);
    }
    ...
    void foo(Lookup<? extends Number> table) {
        // ...
    }
 
- default access control:
    package-private
 
- methods with variable numbers of arguments:
    public static void print(String... messages) {
        // ...
    }
    public void setOrbiters(Body... bodies) {
        // ...
    }
    ...
    mars.setOrbiters(phobos, deimos);
 
    - parameter bodies is of type Body[]
    - can be also called:
        Body[] marsMoons = {phobos, deimos};
        mars.setOrbiters(marsMoons);
 
- testing for type
    if (sref instanceof More)
        mref = (More) sref;
 
    public static void sort(List list) {
        if (list instanceof SortedList)
            return;
        // else sort the list
    }
 
- The Object Class:
    public boolean equals(Object obj);
    public int hashCode();
    public Object clone() throws CloneNotSupportedException;
    public final Class<?> getClass();
    public void finalize() throws Throwable;
    public String toString();
 
- there are three ways of object cloning:
    1) implement Cloneable interface
    2) use default clone method implementation of the Object class
    3) throw CloneNotSupportedException, which signals that clone method
       should not be invoked on this object
 
    - note: shallow vs. deep cloning
 
- interfaces:
    class Point implements Comparable<Point> {
        // ...
        public int compareTo(Point p) {
            // ...
        }
    }
 
    class Sorter {
        static Comparable<?>[] sort(Comparable<?>[] list) {
            // sort algorithm...
            return list;
        }
    }
 
- inner classes = non-static nested classes
- extending inner classes:
    class Outer {
        class Inner { }
    }
    class ExtendedOuter extends Outer {
        class ExtendedInner extends Inner { }
        Inner ref = new ExtendedInner();
        // ...
    }
- local inner classes (such as C++ local classes)
- anonymous inner classes (we know)
 
- classes nested in interface:
    interface ChangeTable {
        class Record {
            public Object changer;
            public Object changeDesc;
        }
        Record getLastChange();
        // ...
    }
 
- modifiable variables in interfaces:
    interface SharedData {
        class Data {
            private int x = 0;
            public int getX() { return x; }
            public void setX(int newX) { x = newX; }
        }
        Data data = new Data();
    }
 
- enumeration types:
    enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
 
    enum Suit {
        CLUBS, DIAMONDS, HEARTS, SPADES;
        public static int getSize() { return 4; }
    }
 
- every enumeration type has two static methods:
    public static E[] values();
    public static E[] valueOf(String name);
 
- enumeration type constant declarations
    enum Suit {
        CLUBS{"CLUBS"),
        DIAMONDS("DIAMONDS"),
        HEARTS("HEARTS"),
        SPADES("SPADES");
 
        String name;
        Suit(String name) { this.name = name; }
        public String toString() { return name; }
    }
 
- enumeration type constant specific behavior
    // #1
    enum ChessPiece {
        PAWN, ROOK, BISHOP, KNIGHT, KING, QUEEN
    }
    class ChessRules {
        Set<Position> reachable(ChessPiece type, Position current) {
            if (type == ChessPiece.PAWN)
                return pawnReachable(current);
            else if (...)
                return ...
            // ...
        }
        // ...
    }
 
    // #2
    enum ChessPiece {
        PAWN {
            Set<Position> reachable(Position current) {
                return ChessRules.pawnReachable(current);
            }
        }
        ROOK {
            Set<Position> reachable(Position current) {
                return ChessRules.rookReachable(current);
            }
        }
        // ...
    }
 
- all enumeration types extend java.lang.Enum
    - can not be extended directly
    - clone is not supported
    - hashCode and equals methods are declared final
    - compareTo method of interface java.lang.Comparable is implemented to
      return natural (declaration) order of enum constants
    - there are two more methods in Enum class:
        public final int ordinal();
        public final class<E> getDeclaringClass();
 
- primitive types: byte, char, short, int, long, boolean, float, double, (void)
- wrapper classes:
    Object <- Boolean, Character, Number, Void
    Number <- Byte, Short, Integer, Long, Float, Double
 
- in many contexts there is an automatic conversion between primitives and
  their wrappers:
    Integer val = 3;
 
- each wrapper class:
    - has static methods:
        static Type valueOf(String str)
        static type parseType(String str)
        static String toString(type val)
 
    - has non-static method: type typeValue()
    - throws exception: NumberFormatException
    - implements Comparable<T> interface
 
- Void class
    - wraps no value
    - can not be instantiated
    - used in reflection
 
- Integer class (and similar)
    - can decode/encode string in custom radix
    - usual arithmetic operations
 
- operators can not be overloaded
- enhanced for statement:
    for (Type loop-variable: set-expression)
        statement
 
- labels
    - labeled break, continue
    - unlabeled break, continue
 
- generic types
    - restrictions:
        - can not be used for static fields
        - can not be used for type instantiation (new T)
        - can not be used for array of types instantiation (new T[size])
 
    - can be nested in the same way as classes
    - generic invocation and type interface:
        <T> T passThrough(T obj) {
            return obj;
        }
 
        String s1 = "Hello";
        String s2 = this.<String>passThrough(s1);   // have to use this!!
        String s3 = passhTrough(s1);
 
        Object o1 = passThrough(s1);            // T => String
        Object o2 = passThrough((Object) s1);   // T => Object
 
        String s4 = passThrough((Object) s1);   // INVALID: will not compile
        String s5 = (String) passThrough((Object) s1);  // OK
 
    - willcards:
        List<? extends Number> list
        static <T> List<T> synchronizedList(List<T> list) {...}
 
        - note: List<?> is not compatible with List<T>
 
    static <T> void processListOfLists(List<List<T>> List) {...}
    static <T> void addToList(List<T> list, T t) {...}
    <S, T extends Number> void m(S key, T val) {...}
 
- exceptions
    try {
        // ...
    }
    catch(Exception e) {
        e.printStackTrace();
    }
 
- assertions
    assert tail.next == null;
    assert (distance >= 0) : "Negative distance";
 
    // NEVER DO THIS!!!
    assert ++i < max;
 
    // control flow assertions
    assert false : "can't find " + val;
 
    // same as (note Error, not Exception!!)
    throw new AssertionError("can't find " + val);
 
    - assertions are turned off by default
    - VM command-line parameters:
        -enableassertions/-ea
        -disableassertions/-da
 
        -enableassertions:com.acme...
        -enableassertions:com.acme.Plotter
        -enableassertions:com.acme... -da:com.acme.Evaluator
 
    - making assertions required:
        static {
            boolean assertsEnabled = false;
            assert assertsEnabled = true;
            if (!assertsEnabled)
                throw new IllegalStateException("Asserts required");
        }
 
 
- endpoint at page 305