Top Links You Must Click On
Java Developer's Journal: "Developing in Java 5"
How to use the new set of tools wisely
Jan. 8, 2005 03:45 PM
You could also use an interface for the same purpose. This is common, but it's debatable whether it's a good idea to do so.
This implementation has many problems. Since the constants are just ints, it's easy to pass out-of-range values to methods. There is nothing to prevent you from passing these constants to methods they weren't designed for.
You can write something like new Card(KING,HEARTS) and you won't find out that something is wrong until runtime.
It's possible to use the "typesafe enum" pattern (for example, see the book Effective Java by Joshua Bloch) in JDK 1.4 to overcome these problems, but it requires a lot of boilerplate code, and the resulting classes have a fatal flaw, from the developer's point of view: you cannot switch on this kind of enum, so any switch-like code becomes very clunky.
J2SE introduces the enum keyword to specify first-class enumerated types. Here is simple example code:
A significant advantage of the enum construct (compared to handcrafted enumerations) is that enums are full-fledged Java classes, so they can have constructors, fields, and methods. For example:
Client code can switch on the enum:
The default case in the switch is used in case someone adds a new polygon to the enum.
Queue and Implementations
Formatter The Formatter class and its cousin methods String.format() and PrintStream.printf() provide a straightforward and powerful way to finely format strings and output. If you are familiar with the C printf and sprintf functions, it should be easy to use the new class and methods. For example, the following code will print out an amount of money using the correct padding if the cents are a one-digit number:
The Scanner class provides an easy way to extract strings and primitives from a formatted stream. By default, it uses white space for field delimiters, but it's easy to configure it to use any regular expression as a delimiter. J2SE 1.4 classes and methods such as StringTokenizer, String.split(), and Integer.parseInt() could cover some of the functionality that the Scanner class provides, but the new class is more flexible and powerful. The following code will echo lines from System.in to System.out, as long as they can be parsed as integers:
Often, being a programmer is much like being a carpenter. To do a job well, you need to know which tools are available and which one is best suited for the job at hand. J2SE 5 provides a set of new tools that you can add to your Java development tool belt. Use them wisely, and you will spend more time thinking about how your code hangs together and less writing boilerplate and tracking down bugs.
Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week