|
|
YOUR FEEDBACK
SYS-CON.TV |
TOP LINKS YOU MUST CLICK ON General Java
Java Developer's Journal: "Developing in Java 5"
How to use the new set of tools wisely
Jan. 8, 2005 03:45 PM
Digg This!
Page 2 of 2
« previous page
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. For example: 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.
Class Libraries
Queue and Implementations
ProcessBuilder 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: Scanner 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: Conclusion 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. References
Page 2 of 2 « previous page
LATEST ECLIPSE 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 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||