Archive for May, 2005

 

Notes (Part 3) from HeadFirst Java..

May 14, 2005 in Books, Java, Uncategorized




  • — Basic network read pattern:
    Socket socket = new Socket("someserver.com", 5000);
    InputStreamReader isr = new InputStreamReader(socket.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    String message = reader.readLine();
    

    — Basic network write pattern:

    Socket socket = new Socket("someserver.com", 5000);
    PrintWriter writer = new PrintWriter(socket.getOutputStream());
    writer.println("message to send");
    
  • — Basic thread pattern
    public class RunnableJob implements Runnable {
      public void run() {
        ...
      }
    }
    
    RunnableJob rj = new RunnableJob();
    Thread thread = new Thread(rj);
    thread.start();
  • — Thread data access synchronization
    — If we synchronize two static methods in a single
    class, a thread will need the class lock to enter either
    of the methods.
  • — Collections
    — ArrayList
    — TreeSet – elements sorted, no duplicates.
    — HashMap – name/value pairs.
    — LinkedList – better performance for insert and delete of elements.
    (better for large data sets)
    — HashSet – no duplicates, fast search by key.
    — LinkedHashMap – same as HashMap plus preserves order of addition.
  • — Basic sorting pattern:
    ArrayList slist = new ArrayList();
    slist.add("a string");
    slist.add("...");
    Collections.sort(slist);
    

    New classes to be used with ArrayList must implement
    “Comparable” (self compare).

    See the use of a comparator (call it MyCompare) which
    implements the compare(MyObject, MyObject)
    method, like this:

    Collections.sort(theList, new MyCompare());
    
  • — Collection types summary:
    — List – sequence.
    — Set – uniqueness.
    — Map – key search.
  • — HashSet duplicate check methods:
    hashCode(), equals().
  • — Control on polymorphic ‘collection type’ usage..
    public void takeAnimals(ArrayList<? extends Animal> animals) {
      ...
      animals.add(someAnimal); // <-- add is forbidden by the '?' wildcard
    }
    
  • -- Static nested classes have access to static variables
    of the enclosing class.

    -- Anonymous nested classes have peculiar syntax capabilities:

    button.addActionListener(new ActionListener() {
      public void actionPerformed() {
        System.exit(0);
      }
    }
    );
    

    ActionListener is not a class, is an interface but in
    this context the 'new' instruction means 'create an
    anonymous class and implement the ActionListener
    interface'.

  • -- Access levels and modifiers:
    -- public - access by anybody.
    -- protected - same package + subclasses in or out of
    the same package.
    -- default - same package only.
    -- private - same class only.
  • -- Enumerations - a set of constant values that
    represent the only valid values for a variable.

    public enum Members { JERRY, BOBBY, PHIL };
    public Members bandMember;
    ...
    if (bandMember == Members.JERRY) {
     ...
    }
    

Notes (Part 2) from HeadFirst Java..

May 08, 2005 in Books, Java, Software




  • — Some methods of class Object
    equals(), getClass(), hashCode(), toString().
  • — A way of thinking about superclasses vs interfaces:
    Superclasses implement types, interfaces implement roles.
  • — About the Constructor
    — It is used to initialize the state of a new object.
    — If none is written, the compiler will create a default one (with no args.)
    — If one is written (any one) the compiler will not supply the default one.
    .. ergo ..
    — If a no-arg one is wanted and there is at least another one, the default
    one must be written.
    — It is good to always supply a default one with default values.
    — Overloaded ones must have different arguments (just like any other method).
    — Argument list concept includes order and/or type of arguments.
    — If the nature of the class is such that the object MUST be initialized
    (such as the Color class), do not write a default one.
  • — Every constructor can call super() or this() but never both.
  • — To prevent a class from being instantiated into objects,
    mark the constructor ‘private’. For example, many ‘utility’
    classes have only ‘static’ methods and they should not be
    instantiated.
  • — Finals..
    — A final variable cannot have its value changed.
    — A final method cannot be overriden.
    — A final class cannot be extended.
  • — Serialization..
    — Write object basic pattern:

    .. class SomeClass implements Serializable ..
    SomeClass sc = new SomeClass();
    try {
      FileOutputStream fs = new FileOutputStream("sc.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fs);
      oos.writeObject(sc);
      oos.close();
    }
    catch(Exception exc) {
      ...
    }
    

    — If a variable in a ‘Serializable’ class cannot (or should not)
    be serialized, it must be marked ‘transient’.

    — Read object(s) basic pattern:

    FileInputStream fis = new FileInputStream("foo.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object one = ois.readObject();
    Object two = ois.readObject();
    ...
    SomeClass sc = (SomeClass) one;
    SomeOtherClass soc = (SomeOtherClass) two;
    ...
    ois.close();
    

    — Static variables are not serialized.

    — Class changes (versions) may break serialization.

  • — Java new IO (nio) features
    — IO performance improvements by using
    native IO facilities.
    — Direct control of buffers.
    — Non-blocking IO capabilities.
    — Existing ‘File[Input|Output]Stream classes
    use NIO internally. Some NIO features may be
    accessed via the ‘channels’.