September 2004 Archives

Java 5.0 FCS is out

New regular expression feature of Java 5.0

The java.util.regex package has been enhanced in Java 5.0. One nice new feature is the ability to save the results of a find() operation into a MatchResult object. Prior to Java 5.0, you could query a Matcher for the results of the most recent find() , but the results would be overwritten by the next find().

The new MatchResult interface lends itself to code like the following:

import java.util.regex.*;
import java.util.*;

public class FindAll {
    public static void main(String[] args) {
	Pattern pattern = Pattern.compile(args[0]);
	String text = args[1];

	List<MatchResult> results = findAll(pattern, text);
	for(MatchResult r : results) {
	    System.out.printf("Found '%s' at (%d,%d)%n",
			      r.group(), r.start(), r.end());
	}
    }

    public static List<MatchResult> findAll(Pattern pattern, CharSequence text)
    {
	List<MatchResult> results = new ArrayList<MatchResult>();
	Matcher m = pattern.matcher(text);
	while(m.find()) results.add(m.toMatchResult());
	return results;
    }
}

This code also demonstrates generics, the for/in loop, and the printf() method, all of which are also new in Java 5.0

Hex Floating Point: 0Xf.aP1 == 31.25

Ugh! I've been following the new language features of Java 5.0 very, very closely, but this ugly one slipped right by me.

We can now write floating point literals using hexadecimal notation. This allows literals to map directly to the bits of a float or a double, I guess.

Here are the unpleasant details: A floating-point literal in hexadecimal notation begins with "0X" or "0x". This is followed by the hexadecimal significand. The catch is that this significand may include a decimal point (a hexadecimal point?). After the significand comes the exponent, which is required. Instead of using e or E to introduce the exponent, hexadecimal floating-point literals use p or P. (Think "power" as a mnemonic). The p or P is followed by the exponent, which must be a decimal number, not a hexadecimal number. And this is a binary exponent, not a decimal exponent. That is, is represents the power of two to which the significand should be raised. Finally, the whole thing can be followed by an f or F to indicate a float literal or a d or D to indicate a double literal, just as a decimal floating-point literal can.

Here are some examples:

public class Hex {
    public static void main(String[] args) {
	double x = 0XaP0;    // 10 * 2^0 = 10
	double y = 0XfP2D;   // 15 * 2^2 = 60
	float z  = 0Xf.aP1F; // (15 + 10/16ths) * 2^1 = 31.25
	System.out.printf("%f %f %f%n", x, y, z);
    }
}

This change is a Sun-internal one, and was not the result of any JSR through the Java Community Process.

1000 US Soldiers Dead in Iraq

This is a sad milestone.

Data from icasualties.org

To display this list on your own website, grab these two files:

This works best in Mozilla. If you know how to get the fading code to work in IE, let me know and I'll fix it.

Update: I've written JavaScript code to display a histogram of the ages of the dead:

Java 5.0 RC is out

The Java 5 Release Candidate is now available.

Books

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

The classic Java quick-reference