Saturday, August 28, 2010

Double.valueOf does NOT cache instances!

I had one of those lightbulb moments where I asked myself, "You know, I wonder how Double.valueOf implements the caching... Or does it???".

The documentation is an obvious place to look for information, of course:
public static Double valueOf(double d)

Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.
No obvious "frequently requested values" come to mind, aside from perhaps the NaN, the signed zeroes, the signed ones, and the signed infinities. Perhaps MIN/MAX_VALUE and MIN_NORMAL are also worth caching, since they at least do have the privilege of being defined as class constants.

Either way, it's not as simple as having an array the way e.g. Integer instances are cached by Integer.valueOf for values around zero.

In my pursuit to uncover what is actually cached and how, I went straight to the source code.
public static Double valueOf(double d) {
    return new Double(d);
Ladies and gentlemen, we have been duped.

Saturday, August 21, 2010

Fluid dynamics: a code formatting experiment

Currently experimenting with fluent interface formatting. Specifically, how should you format it when you are switching context in the middle of a chain? (Should you even do that in the first place?)

   new StringBuilder()
         .replaceAll("([a-z]+)\\1", "$1")

Wednesday, August 11, 2010

(Unofficial) Errata for the Java Language Specification 3rd Edition

I don't think there's an official errata for the 3rd Edition, so here's my (half-hearted) attempt to compile one.

5.1.4 "…combines both widening and narrowing primitive convesions…"
4.8 "…a raw type is define to be…"
15.8.2 "…a class literal, C.Class, where…"
8.4 "…but this is discouraged as a matter of syle."

There are also some more serious non-typographical errors/omissions/inconsistencies in the Java programming language grammar given throughout the document when cross referenced with 18.1.

See also: - Errata for Java Language Specification 3rd Edition

By the way, those section numbers aren't linked, but is still up (see blog entry: OpenSearch for JLS).

Tuesday, August 3, 2010

Egex-ray astardization-bay

A (loosely specified) Pig Latin regex translator:

String[] words = "ask another stupid question please".split(" ");

for (String word : words) {
   System.out.printf("%s -> %s%n", word,
      ("w" + word).replaceAll(
This prints (as seen on
ask -> ask-way
another -> another-way
stupid -> upid-stay
question -> estion-quay
please -> ease-play

Sunday, August 1, 2010

Rickrolling regex

   ("Never gonna give you up, let you down, run around and desert you." + "\n" +
    "Bob Dole is a friend of the tobacco industry, likes your style,,,."
   ).replaceAll("(?m)(?<=^(\\w+ \\w+).*,)", "\n$1")
This prints (as seen on
Never gonna give you up,
Never gonna let you down,
Never gonna run around and desert you.
Bob Dole is a friend of the tobacco industry,
Bob Dole likes your style,
Bob Dole,
Bob Dole,
Bob Dole.