Saturday 10 May 2008

Java Puzzlers by Bloch & Gafter

Summary of most important things in this book:

  • To escape characters in regular expression use Pattern.quote("."). To change replacement string Matcher.quaoteReplacement(String).
  • In java are statemant labels
  • Special values:
Double.NaN
Double.POSITIVE_INFINITY
  • Be careful:
Integer.MIN_VALUE = -Integer.MIN_VALUE
Long.MIN_VALUE = - Long.MIN_VALUE
  • Java exception checking is not enforced be the virtual machine
  • Class.newInstance can throw checked exceptions that it does not declare
  • Generic type information is checked at compile time not at runtime.
  • When a program calls a static method, the method to be invoked is selected at compile-time, based on compile-time type of a qualifier
  • Static methods annnot be overridden, they can only be hidden
  • Never call overridable methods from constuctors
  • AtomicLong - object with implementation for synchonisation
  • You must override hashcode() whenever you override equals
  • Integer literals begining with 0 are in interpreted as octal values
  • Math.abs is not guaranteed to return nonnegative result. For Integer MIN_VALUE and for Long MIN_VALUE it does not
  • Methods in object are overwritten and fields are only hidden but can be other types.
  • It is possible to hide variables, nested types and static methods.
  • A package-private method cannot be directly overridden by a method in a different package
  • Thread.join calls Object.wait on the Thread instance representing the Thread being joined. This releases the lock for the duration of the wait
  • You cannot legally access a member of a nonpublic type from an other package
  • The constructor of a non-static nested class is compiled such that it has as its first parameter an additional implicid parameter representing the immediately enclosing instance. So avoid using reflection to instantiate inner classes
  • PrintStream.write(int) is the only output method that does not flush a PrintStream
  • You must drain the output stream of a child process in order to ensure its termination. The same goes for the error stream.
  • Calling Thread.interrupted always clears the interrupted status of the current thread
  • References to constant fields are resolved at compile time to the constant values they denote.