Comments
yourfanat wrote: I am using another tool for Oracle developers - dbForge Studio for Oracle. This IDE has lots of usefull features, among them: oracle designer, code competion and formatter, query builder, debugger, profiler, erxport/import, reports and many others. The latest version supports Oracle 12C. More information here.
Cloud Expo on Google News

2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Java Developer's Journal: "Developing in Java 5"
How to use the new set of tools wisely

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:


public class Card {
public Card (int suit, int rank) {
...
}
}
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:


public enum RegularPolygon {
TRIANGLE,
SQUARE,
PENTAGON;
}
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:

public enum RegularPolygon {
TRIANGLE(3),
SQUARE(4),
PENTAGON(5);
final int numSides;

RegularPolygon (int sides) {
numSides = sides;
}
public double perimeter (double sideLength) {
return sideLength * numSides;
}
}
Client code can switch on the enum:

public computeArea(RegularPolygon p, double side) {
switch (p) {
case Polygon.TRIANGLE:
return side * side * Math.sqrt(3) / 4;
case Polygon.SQUARE:
return side * side;
case Polygon.PENTAGON:
return side * side * 1.72;
default:
System.err.println("Unknown polygon");
return 0;
}
}
The default case in the switch is used in case someone adds a new polygon to the enum.

Class Libraries
J2SE 5 introduces several utility classes that can make your life easier, but you obviously won't gain any benefits from them unless you actually use them. It is almost always a mistake to write your own implementation of a data structure or a method that is present in the Java class library. The base Java implementation is very likely to be better thought-out and less buggy than an implementation that you would come up with. Here are a few new additions to the Java class libraries that may prove useful.

Queue and Implementations
java.util.Queue is a new interface that has been added to the Collections framework. LinkedList now implements the Queue interface and models a simple FIFO queue. Another implementation you may find useful is PriorityQueue.

ProcessBuilder
The ProcessBuilder class provides a significantly easier way to launch and control applications than Runtime.exec() does. The following code will launch Eclipse on a Linux machine from a specified directory and display its window on the main display of the machine called italy.


String home = System.getProperty("user.home");
ProcessBuilder builder = new ProcessBuilder (
"./eclipse", "-data", home + "/workspace");
builder.directory(new File(home + "/eclipse"));
builder.environment().put("DISPLAY", "italy:0.0");
builder.start();
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:

public static void dollarFormat (int dollars, int cents) {
System.out.printf("$%d.%02d", dollars, cents);
}
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:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNextInt()) {
System.out.println(scanner.nextInt());
}
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

About Roberto Scaramuzzi
Roberto Scaramuzzi is a software engineer at Parasoft. He grew up professionally as a Perl developer, but is now also adept at Java and PHP. Roberto holds a PhD in mathematics.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

'Ease of Development' is one of the main focuses in J2SE 5. Accordingly, J2SE 5 introduces several new features designed to simplify the developer's life. If you use these new constructs, your code will become more compact and expressive, hence easier to understand and debug. This article explains how you can use the new features to prevent some silly mistakes, as well as some that are not so silly.


Your Feedback
Java Developer's Journal News Desk wrote: 'Ease of Development' is one of the main focuses in J2SE 5. Accordingly, J2SE 5 introduces several new features designed to simplify the developer's life. If you use these new constructs, your code will become more compact and expressive, hence easier to understand and debug. This article explains how you can use the new features to prevent some silly mistakes, as well as some that are not so silly.
Enterprise Open Source Magazine Latest Stories . . .
Kubernetes is a new and revolutionary open-sourced system for managing containers across multiple hosts in a cluster. Ansible is a simple IT automation tool for just about any requirement for reproducible environments. In his session at @DevOpsSummit at 18th Cloud Expo, Patrick Galbr...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one lo...
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, will provide an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSy...
Kubernetes is a new and revolutionary open-sourced system for managing containers across multiple hosts in a cluster. Ansible is a simple IT automation tool for just about any requirement for reproducible environments. In his session at @DevOpsSummit at 18th Cloud Expo, Patrick Galbrai...
Learn how to solve the problem of keeping files in sync between multiple Docker containers. In his session at 16th Cloud Expo, Aaron Brongersma, Senior Infrastructure Engineer at Modulus, discussed using rsync, GlusterFS, EBS and Bit Torrent Sync. He broke down the tools that are need...
It is ironic, but perhaps not unexpected, that many organizations who want the benefits of using an Agile approach to deliver software use a waterfall approach to adopting Agile practices: they form plans, they set milestones, and they measure progress by how many teams they have engag...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021




SYS-CON Featured Whitepapers
Most Read This Week
ADS BY GOOGLE