Wednesday, 10 December 2008

Exceptions to Java java.lang.Exceptions

The compile error: "unreported exception java.lang.Exception; must be caught or declared to be thrown" forces you to add a throws clause to a method that can throw an exception.

That's not the end of the story. If this class overrides an interface method, the throws clause must be declared at the interface level. Using Eclipse compiler it gives: "Exception is not compatible with throws clause in Interface.method()". Using Sun compiler you get a slightly different message but the intent is the same. Irritating.

What next in Java 7?

Good article on adding closures to Java. An interesting comparison is made between closures and inner classes. The article is by Angelika Langer. Closures are a very beautiful way of implementing mathematics. For example, a bisection algorithm can be elegantly implemented with closures.

To understand the limitations of inner classes versus closures we need to understand exactly the rules for inner classes in Java. An inner class can just a private class inside another class (like an embedded struct). Anonymous inner classes are used when creating ActionListeners e.g. button.addActionListener( new ActionListener() {} ).

Sunday, 7 December 2008

The Dichotomy of Mutable and Immutable in Java

String and StringBuffer are immutable and mutable analogues of one another. StringBuffers are used to implement the binary string concatenation operator.

Example: x = "a" + "b" + "c";
is compiled to x = new StringBuffer().append("a").append("b").append("c").toString();

You can also call append on ints, chars, floats and doubles.

When you instantiate a StringBuffer it has an initial capacity of 16 characters, however e.g. new StringBuffer(500) will create a StringBuffer with an initial capacity of 500 characters.

An interesting article on a mutable and immutable design pattern (as exemplified by String and StringBuffer) can be found on javaworld.

Java 6 Platform Overview

http://java.sun.com/javase/6/docs/

A very interesting tool that comes with Java 6 is the VisualVM. Details of how to run it are here.

And here is a getting started guide to VisualVM.

Saturday, 6 December 2008

Java Server Development with java.net

java.net.ServerSocket is a class for implementing server sockets. ServerSockets can be created unbound, or bound to a particular port. In either case, the constructor may throw an IOException, which may be thrown when attempting to open the socket. The most important method on a server socket is accept(), which listens for a connection to be made to the socket and accepts it. Example code (notice the interplay between the ServerSocket and Socket classes):

import java.net.ServerSocket;
import java.net.BindException;

ServerSocket listenSocket = null;
try {
listenSocket = new ServerSocket(serverPort);
while(true) {
// listen for and accept incoming connection
Socket clientSocket = listenSocket.accept();
// do something
}
} catch (BindException bindEx) {
// error message to say server is already running on serverPort
System.exit(0);
}

The socket class abstracts the concept of an endpoint of communication between two machines. A nice visual analogy of the socket idiom is the probe-and-drogue method of aerial refuelling used by the USAF. Pictures can be seen on aerospaceweb.

SSLServerSocket is a special instance of ServerSocket; these sockets aer generally created by an SSLServerSocketFactory.

Tactics for Analysing Complex Java Code

Complex Java code can be analysed quickly using the following methods.

1. Create a temp file with the code in Visual J# Express. Compress the code, refactor logically.
2. Use the program and get an idea of how it works: inputs and outputs.
3. Identify critical control paths, frequently used and unused areas of the code. (perhaps only 20% of the code is actually interesting or relevant).
4. Write up the program in Python-style pseudocode.
5. Use set theory concepts (e.g. partitions) to break up the code into mutex sections (e.g. partitions of the input space, partitions of inner classes etc).