ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Keeping Up with the Java Joneses
Pages: 1, 2

printf is Back

In the early days of Java, it was common for people to try to bring the C-language printf functionality to Java. Most of these attempts worked only for certain cases, and were not really object-oriented approaches. After much complaining from developers and much internal debate, Sun has relented and included printf functionality into J2SE 1.5.

The functionality is contained in the new java.util.Formatter class (not to be confused with the existing DateFormat, NumberFormat, etc., classes) and is also available in convenience routines in System.out (actually, in the PrintStream and PrintWriter classes). The format codes are more comprehensive than the original C printf, but the basic idea is the same: you pass a format string and one or more objects to be formatted according to the format string. For example, you might write:

System.out.print("Pi is approximately %1$6.4f\n", Math.PI)

The % is the lead-in to the format code; the 1$ means it is to format the first argument after the format code (the value Math.PI), and the 6.4f is the same as it was in printf and in Fortran before that -- a floating point value with a field-width of six and four digits after the decimal place. So the program prints:

Pi is approximately 3.1416

There is much more to this, with support for date formatting, localization, and more. See the documentation for java.util.Formatter for details.

Oh, and did I mention that there is also scanf-like functionality, in java.util.Scanner? This does not use % format codes, but uses a variety of next() methods, such as next(String), next(Pattern), nextInteger, nextDouble, and so on, plus all of the corresponding hasNext() methods. See the documentation for java.util.Scanner. printf and scanf are covered in recipes 10.3 and 10.4 of Java Cookbook, 2nd Edition.


Ever since the early days of Java, developers have had many IDEs from which to choose. Today, there are several commercial ones and two major open source ones, both of which are from big companies and both of which are used in the companies' commercial IDEs. Sun's NetBeans is the open source version of their various Studio products, including the new Creator tool for building JSF-based web sites. And Eclipse is the open source version of IBM's WebSphere developer products.

At least, it started out that way. But sometimes things take on a life of their own. Both NetBeans and Eclipse have done so, leading to NetBeans.org and the Eclipse Foundation.

Both NetBeans and Eclipse are good developer tools. Eclipse does seem to have the edge in developer momentum. A quick search on Amazon.com, for example, shows a dozen or so Java books with Eclipse in the title and only one with NetBeans in the title. However, NetBeans has a more mature GUI builder (surprisingly, Eclipse's GUI builder is still in beta as I write this). Eclipse has grown quickly, in part, because it seems to do more to encourage development of plugins. And the recent release of Eclipse 3.0 promises to bring some stability to the IDE, after a number of rapid changes over the last few "milestone builds."

So if Eclipse is somehow better, should Sun throw in the towel? No way! Because, as OpenBSD founder Theo de Raadt says of operating systems, "Choice is good." This is certainly true in IDEs as well.

But If I Had to Pick One

I've long been fond of using a text editor (vi) and command-line Java. I shunned the early Java IDEs because of stability problems in some (one which shall remain nameless, but was nicknamed Visual Crashé), because of "version lag" (it took most of the IDE vendors a long time to move from 1.1 to 1.2, because of the new event model, just as it is taking them a while to upgrade to 1.5 because of the compiler changes) and -- in the early days -- the blazing lack of speed.

Most of these things have changed, and so recently, I took up the tools again, trying both NetBeans 3.6 and Eclipse 3.0. I was pleasantly surprised by the productivity gains they offered. I settled on Eclipse because of a project at one of my major clients and because of the number of books available.

I have found moving to Eclipse to be very worthwhile. An IDE is a great place to "encapsulate" all of those finger-twisters for which Java is becoming noted. Eclipse features a lot of shortcuts. For example, to convert a List into an Array, you probably know that (at least in 1.4) you have to call the toArray method, pass in an array of the right length, and cast the result back. In Eclipse, you can just type, for example, newArray = toArray and hit the Completion sequence (Ctrl-Space); and the IDE will fill in:

newArray = (type[]) collection.toArray(new type[collection.size()])

Also, it positions the cursor on type and highlights it, so you can just type, say, String -- and it will change it both places. Then you hit the Tab key to move to collection, and type the collection's name, say, myList, and you are finished. You now have:

newArray = (String[]) myList.toArray(new String[myList.size()])

And that's just from one shortcut -- there are many others defined. To see the shortcuts that are defined, as well as to create your own, go to the Preferences Window, then Java, Editor, and then Templates. Eclipse also has powerful code completion and correction features. For example, if you type File f = new File(someFileName); and you haven't imported the File class from java.io, you'll get an error. But rather than making you wait until you run the compiler or Build Project, Eclipse will immediately highlight the error in red at the left margin, and also underline the references to the File class.

Eclipse highlights the error
Eclipse highlights the error

Just press Ctrl-1 and Eclipse will open a small context menu, one of whose choices is "Import File (java.io)".

Eclipse lets you fix the error quickly
Eclipse lets you fix the error quickly

Once you do that, the code compiles nicely.

Eclipse lets you fix the error quickly

And once you save the file, there is no separate compile step! Eclipse builds the class file dynamically while you are editing it, and when you save, it saves both the Java form and the bytecode form. So once you've dealt with all of the red (and, hopefully, yellow) icons, and saved your file, you're finished; time to head out for a decaf latte!

When you get back from coffee, you'll want to check out Eclipse's JUnit integration, which is also excellent. Eclipse will build test cases on demand; just give it the name of the class to test. If the class exists, Eclipse will offer you a list of methods for which to write test methods. If not, write your test methods and get Eclipse to generate the methods in the class being tested -- great for test-first development! Eclipse will build and re-build your test case dynamically, letting you keep local changes but also maintaining the list of tests to run from Eclipse's GUI.

Especially if you've always been a command-line junkie like me, I recommend that you give Eclipse or NetBeans a fair trial. Download Eclipse or NetBeans (not over a 56K dialup connection) or order them on CD. Pick up one of several good books on Eclipse (including O'Reilly's Eclipse), and learn your way around, including how to configure options, how to use the error handling and code completion, and "organize imports." Check out the Refactor menu. Then try importing one of your projects, or start a new project, and build some code. Give it a try and I think you'll like it.

Aspects of Java

One of the key things you should probably try to learn about is a whole new area including lightweight frameworks and the Inversion of Control (IoC) pattern (also called Dependency Injection), and Aspect-Oriented Programming (AOP).

IoC itself is used by many frameworks -- it basically means that the framework calls your code instead of vice versa.

Lightweight frameworks such as PicoContainer and the Spring framework go further and allow various components to be constructed automatically, with information being passed (or "injected") into the objects, either by "Constructor Injection" or "Setter Injection." Aspect-Oriented Programming is a way of applying "aspects" -- things that don't fit well into the method-based design of objects -- to your code.

The Spring framework is both a light-weight framework and an AOP toolkit; another AOP package is AspectJ. See this ONJava article on AOP in Spring, which has links to more information on Spring and AOP.

Keeping Ahead of the Curve

I've talked about some of the new technologies happening in the Java world. There are so many; how is a busy developer to keep up with all the changes? I hope this article has helped. The O'Reilly site ONJava.com is a good resource, and O'Reilly Java books are the thing to have on your bookshelf (or your virtual bookshelf, if you prefer to read them on Safari). Don't forget the Sun/O'Reilly site java.net.

Producer's Note: Today Safari and CIGNEX Technologies, Inc. have announced that they have created an Eclipse plugin that will allow Eclipse users to search Safari content from within the Eclipse environment. It currently works with Eclipse version 2.1.3 and will soon be available in version 3.0. The plugin and source code are available as a project in the Java Tools Community on java.net: safarieclipse.dev.java.net.

Other resources include JavaWorld.com, Sun's Java Developer Center (and in particular, their JDC Tech Tips Newsletter), and technology-specific resources such as TheServerSide.com, which specializes in server-side Java.

While it's not the place for leading-edge technology, The Java Lobby is a good place to keep up on general happenings. There are several forum sites where you can ask questions and get answers on specific Java technologies; these include Java Ranch (founded by O'Reilly author Kathy Sierra) and jGuru.com.

If you still have any free time, there are Java blogs out there. If you want to skim, most of these resources have RSS feeds (if you're not up on RSS, think of it as "headlines with links;" O'Reilly's Meerkat was one of the leading early adopters of RSS). Even James Gosling has written an RSS reader, and Mac fans will have RSS support in Safari in Mac OS 10.4 (codenamed "Tiger," the same codename that Sun used for J2SE 1.4).

And, of course, you can always get the newest version of my Java Cookbook to have recipes on most of the features discussed in this article, as well as all the common APIs that were in Java before, such as date & time, number formatting, and many more.

There is no shortage of new technologies, and no shortage of ways to try to keep abreast of and make sense of it all.

Ian F. Darwin has worked in the computer industry for three decades: with Unix since 1980, Java since 1995, and OpenBSD since 1998. He is the author of two O'Reilly books, Checking C Programs with lint and Java Cookbook, and co-author of Tomcat: The Definitive Guide with Jason Brittain.

In June, 2004 O'Reilly Media, Inc., released Java Cookbook, 2nd Edition.

Return to ONJava.com.