Java

From DesigningPatterns

Jump to: navigation, search

Contents

Background

Java is a statically-typed language originally created by sun.

Characteristics

  • Java automatically chains constructors, similar to how C++ chains constructors. In particular, a parent class' constructor *always* will be called. This is quite different from Ruby, which does not automatically chain constructors.
    • The no argument ("default") parent class constructor will be called unless a different parent class constructor has been invoked explicitly through the super keyword. Such an invocation must be the first line of the child class constructor.
    • If there is no explicit invocation of the parent class' constructor and the parent class has no default constructor, then the child class will not compile.
  • There are no default arguments (unlike C++ and Ruby).
  • There is no operator overloading (unlike C++ and Ruby).
  • There currently are no closures, although this will be part of Java 7.
    • Some use cases of closures are supported by anonymous local classes.
  • Java does not have named parameters (like Common LISP does). It also cannot simulate them (like Ruby does, with Hashes). There is, therefore, no really clean way to write methods and constructors that need to accept lots of arguments (some of which are optional).
    • Effective Java recommends the builder pattern for this.
  • Static initialization blocks are possible with the static keyword. For example:
static {
  int i = 1;
  ...
}
    • Static initialization is thread-safe and guaranteed to occur once.
  • There are no destructors. Classes can define finalize instance methods that are called by the garbage collector before the object is destroyed, however.
    • There is no guarantee if and when a finalizer will run, so the finalizer should not be relied upon for time sensitive or critical tasks.
    • Finalizers are not chained (except for the Object.finalize), so a subclass must call its parent's finalizer explicitly.
      • A class can prevent a child class from skipping its finalization by having an inner class do finalization.
    • Defining a finalizer for a class can make object creation and deletion for the class less efficient.
    • Uncaught exceptions thrown from a finalizer will be ignored silently.
  • Java is fully garbage collected.
    • An object becomes eligible for garbage collection when no more references to the object exist.
  • Primitive types (int, boolean, float, etc.) have value semantics while objects (everything else) have reference semantics.
    • The primary motivation for this was performance.
    • This is very different from Ruby, where everything is an object, and everything has reference semantics (Ruby cheats, however; its Integers and Floats are implemented as immediate values, not full-fledged objects).
    • This is very different from C++, where everything has value semantics.
    • Java primitives all have corresponding classes (i.e., int and Integer).
      • Since Java 1.5, primitives are converted automatically to and from their associated classes when necessary (called auto-boxing). Auto-boxing can have negative performance implications.

Annoyances

Surprisingly, despite its massive penetration, Java still has a number of very annoying "features". I feel that these features put it at a significant disadvantage to some of its competitors, such as Ruby and Python.

  • The generics system is horribly flawed, due to not having any run-time presence. It is far less usable than C++'s template system.
  • The import system is flawed.
    • There is no way to alias a package name.
    • There is no way to use a partially qualified name (i.e., refer to a.b.c.Class1 as c.Class1 in a.b.Class2). This ensures that class names often have to duplicate information in the package name so that they do not conflict with other classes (i.e., com.designingpatterns.service.FileService, rather than com.designingpatterns.service.File). C++ has had this feature for ages.
    • Static initialization for a class is guaranteed to occur before any calls within the class proceed or before any references to the class are resolved.

Configuration

yum install java-1.6.0*
yum install ant
yum install ant-junit

The javaws link might be bad; check and correct if necessary.

Get the Maven Ant tasks from here (cannot download directly, however, must click through) and install them in ant's lib directory (ANT_ROOT/lib).

Also, configure Tomcat.

Reference

Questions

  • When/how is static initialization accomplished? What kind of thread-safety concerns exist with it?
Personal tools