Author: techfox9
Notes (Part 2) from HeadFirst Java..
Sunday, May 8th, 2005 @ 12:50 am
- — 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’.