Wednesday, October 3, 2012

Java

  1. import static java.lang.Math.max; lets use max(2, 7) in place of Math.max(2, 7);
  2. A derived class does not inherit private methods of the base class.
  3. An overridden method must match the signature (+ return type) of its base class counterpart, except that it can have covariant return type. In the overridden method more permissive access modifier can be used, e.g., private -> public not the other way around.
  4. A final method cannot be overridden and a final class cannot be subclassed.
  5. Most to least restriction: private, default (inside same package), protected (same package + subclass), and public. 
  6. (a instanceof B) returns true even if a's class is a subclass of B. So, if we wish to check if a's class is exactly equal to B, we need getClass() method.
  7. Define catch blocks starting with most specific and ending with most general, e.g., NegativeNumberException should come before Exception, if not, NegatveNumberException never gets caught.
  8. try[-throws]-catch-finally: if no matching catch is there the method ends there, so codes after catch blocks do not get executed. However, finally ensures, even in this case the codes inside it get executed.
  9. ObjectInputStream and ObjectOutputStream let readObject and writeObject from and to file, if object is serializable. In order for a class to become serializable, all its class-type instance variables must be serializable as well.
  10. RandomAccessFile lets access bytes in the file at random.
  11. An abstract class can implement an interface, in that case not all methods mentioned in the interface have to be defined in the abstract class. An interface can extend another interface. All fields in an interface are public, static, and final; usually they are the useful constants. 

Monday, October 1, 2012

Javadoc

A comment must:
1. immediately precede a public item
2. start with /** and end with */
Tags are:
@param name description
@return description
@throws type explanation
@deprecated
@see package_name.class_name
@author name
@version info
usage: javadoc [options] package_name
Options are:
-link link_to_other_docs (may be url to java doc)
-d documentation_directory (where to put after generation)
-author (extracts author info from comments)
-version
-classpath list_of_directories (overrides CLASSPATH)
-private (extracts even the privates, not good)