Strings and Java 5
For six months, developers have been migrating to the Java 5 platform. Here we’re going back to basics to find out what’s new in the Java 5 String class. For the last decade, I’ve had my own string utility library to make up for glaring functional gaps in the Java String class. This year I finally got rid of it – so let’s see how Sun plugged these gaps, and take on board the new guidelines for using strings.
It’s a string thing
Java developers have been warned not to blindly concatenate immutable strings with the special operators ‘+’ and ‘+=’. The mantra is: use a StringBuffer if you’re building a string that spans multiple statements. So:
String bigString = “Java “;
bigString += “Rocks!”;
Becomes:
StringBuffer bigString = new StringBuffer(“Java “);
bigString.append( “Rocks” );
This is still good advice, particularly if you’re building strings inside loops. However, almost all StringBuffers are used to build strings in a single thread.
Now consider that the majority of the StringBuffer methods are synchronised. This means that for 99 times out of 100, string building with StringBuffer is slower than it needs to be. In Java 5, there’s a new StringBuilder class that adheres to the StringBuffer interface, but whose methods are not synchronised. Therefore, you should always use StringBuilder to build strings within the single threaded parts of your application, and use StringBuffer in the other cases.
A simple but useful addition to the String class is the contains() method. Good riddance to:
if( “hello”.indexOf(“ll”) != -1 ) ...
and welcome:
if( “hello”.contains(“ll”) ) ...
Another useful method has finally made into the core API. The Replace method replaces all occurrences of the first parameter with the contents of the second.
“Java 5 frocks”.replace(“frocks”,”rocks”)
It’s a shame that it has taken 10 years for such a key method to make it into the core API. I’m just glad it’s finally there!
Finally, bundled in the java.util package within Java 5 is the Formatter class. This provides an interpreter for printf-style format strings. On the String class, the static format method can be used to format strings. For example:
String.format( “hello you [%1$10s]”, “cat”)
returns the following string. Note how the string bot has been padded out to 10 characters.
hello you [ cat]
The formatter can support many type conversions, flags and precision operations. You should read the appropriate JavaDoc for the Formatter class. Take a look at this snippet that deals with time:
Calendar c = new GregorianCalendar(2005, Calendar.DECEMBER, 25);
String xmas = String.format(“Christmas Day: %1$ta %1$te %1$tb, %1$tY”, c);
// xmas = Christmas Day: Sun 25 Dec, 2005
The ‘%1$’ refers to the first value to be substituted in, which is the ‘c’ for Calendar in this case. The next value is ‘t’, which instructs the format to perform time and date conversions. The final flags (a,e,b and Y) specify exactly what you want pulled out of the calendar object.
DWR 1.0
Finally, there has been a lot of chatter in the developer community about Asynchronous JavaScript and XML, or AJAX. AJAX has breathed some more interactive life into web-based applications by allowing pages to be updated, rather than entirely refreshed. This has opened the door to more user-friendly and highly interactive web pages.
Then, in August this year, version 1 of Direct Web Remoting (DWR) was released. DWR is an easy AJAX implementation for Java, exposing Java server-side objects as JavaScript functions within the browser client. This is an extremely powerful programming model in which Java developers can literally hand over their JavaScript functions to the web developers. From this point on, web developers can call directly into these exposed parts of the application hosted on the web server, using nothing more than plain old JavaScript text. It‘s an important step forward.


