Still using StringBuffer? That’s sooo Java 1.4

Pop quiz: Hashtable is to HashMap as StringBuffer is to ... <fill in the blank>

Answer: StringBuilder.

I recently worked on a Java project where the target environment was Java 1.5. Although Java 1.5 has been out for almost three years, the client was just upgrading to it to take advantage of its language features and APIs.

While working on the project, I noticed most developers continued to use the StringBuffer class when StringBuilder would have been the better choice. In asking around, most developers said they were unaware of StringBuilder.

In case you're using Java 1.5 or 1.6 but not yet using StringBuilder, StringBuilder is an unsynchronized version of the tried-and-true StringBuffer class. Most of StringBuffer's public methods are synchronized to allow multiple threads to read and modify the string simultaneously. But since StringBuffer is almost always used to build up a string within a method, or to build a string over several method calls within a single-threaded environment, the synchronized nature of StringBuffer is overkill. An article in Dr. Dobb's Journal in June 2006 estimated switching from StringBuffer to StringBuilder could speed string building by 38%.

That's why Sun added StringBuilder to the language in JDK 5. None of StringBuilder's methods is synchronized, so the class is not meant to be used when multiple threads need to access the string. In multi-threaded contexts, you will want to use StringBuffer. But consider your own code. How many times have you needed to share a StringBuffer between multiple threads? You'll probably find that StringBuilder is often the better choice.

Independence Day in D.C.

Yesterday saw another great celebration on the National Mall in Washington of our nation's declared independence. Two hundred thirty-one years ago, the Continental Congress adopted Thomas Jefferson's draft of the Declaration of Independence.

'Jefferson' and 'Franklin' read the Declaration
"Thomas Jefferson" looks on as "Benjamin Franklin" reads the Declaration of
Independence on the National Mall in Washington, D.C.
We began the morning at the National Archives, where the original Declaration of Independence is stored, for the annual dramatic reading of the document by men portraying three of the original signers: John Adams, Thomas Jefferson and Benjamin Franklin. Last year, the last couple of paragraphs were read by two men of our armed forces who were wounded in Iraq or Afghanistan. One of the men suffered head injuries, and his reading was stilted and slurred, yet he bravely read through the document. It brought tears to many in the crowd assembled on the steps outside the archives and spilling out onto Pennsylvania Avenue.

This year, they brought a veteran of World War II to read the last part of the Declaration, and filmmaker Ken Burns talked about his upcoming World War II documentary, The War, which recounts the war from soldiers who fought it. I heard no mention of any active war going on, or of any of the men and women fighting in it. Iraq already seems like a war we're fighting to forget.

'Jefferson' and 'Franklin' read the Declaration
Rockets red glare light up the boats on the Potomac River during the
fireworks finale.
We watched a little of the Independence Day parade down Pennsylvania Avenue, walked through the exhibits and listened to music at the Smithsonian Folklife Festival on the Mall, then returned home in the afternoon to watch the fireworks from our balcony.

At around 5 p.m., a lightning storm prompted police to evacuate the open areas of the Mall and the Marine Corps Memorial. Officers asked picnickers and others staking out seats for the concert and fireworks to seek shelter in the various museums and memorials. The storm passed through after about an hour, and the 8 p.m. concert at the Capitol began on time, as did the fireworks an hour later. Last year we watched the fireworks from the Lincoln Memorial. This year, we were able to enjoy the view from our home in Rosslyn.

The fireworks show was great, as usual, but this year I thought it was marred a bit by two orbiting police helicopters, one to the east of the Mall and one to the west. Security was visibly tighter this year, the terror tenor of our times.

And to put another damper on an otherwise perfect evening, three men who put on the fireworks display were hurt and burned, one seriously, when unexploded fireworks went off about 15 minutes after the finale. I was still looking toward the Lincoln Memorial and saw two or three fireworks explode at ground level. May the injured fireworkers recover fully.