If you’ve tracked each major Java release, you probably were eager to see what new packages were in each one and how you could employ them in your Java projects. Along the same lines, the next major Java SE release, Java 7, promises several new features across all packages, such as modularization, multi-language support, developer productivity tools, and performance improvement. I think programmers eventually will begin specializing in individual Java packages (i.e., java.util programmers, java.io programmers, java.lang programmers, etc.), but until then, let’s explore a few of the notable new developer productivity features slated for Java 7.
Find examples of all the features discussed here in the code download for this article.
New Objects Class
The new Objects class of the java.util package provides a fail-proof way for comparing two objects at runtime:
equals()method of the Objects class does a reference comparison.
deepEquals()method piggybacks on the first argument’s
Similarly, when both the arguments are object arrays,
Array.deepEquals() is invoked on the objects. The new Objects class provides all the required static utility methods.
New Classes to Operate on File System
Java SE 7 provides classes that greatly simplify the age old integration processes of one application dropping files at a predefined shared location and other application picking them up. Java 7 provides a new class WatchService that notifies any events that take place in the file system under the watch.
The following steps create an asynchronous file-watcher service:
- Obtain the path from the File class.
Path fPath = new File(filePath).toPath();
- Obtain a handle to the Watch service from the file system.
dirWatcher = fPath.getFileSystem().newWatchService();
- Register which type of events you are interested in.
- Wait for the event to happen.
WatchKey key = dirWatcher.take();
The WatchKey class now has all the details of the event that occurred in the directory.
- Loop through Step 4 to continue receiving events.
New Classes for Concurrency Package
The Java SE team added a wide variety of new classes to Java 7 to cater to various concurrency functionalities. Most notable among them are the RecursiveAction and RecursiveTask classes, which simplify new algorithm development. Understanding the difference between heavyweight and lightweight processes will help you grasp the value of these new classes.
- A heavyweight process gets a replica of the code, stack, and data from the parent process. You create a heavyweight process by invoking
- A lightweight process gets its own stack and shares resources and data with other threads or the parent thread. The Unix Thread API standard (POSIX) provides methods to create a thread.
Java 7 defines a new abstract class called ForkJoinTask, a lightweight process that generates a distinct stream of control flow from within a process. RecursiveAction and RecursiveTask are abstract subclasses of ForkJoinTask.
To code a recursive call, you must subclass either one of these classes and define the
compute() method. The
getRawResult() method returns null for RecursiveAction and returns a value for RecursiveTask. The Java 7 documentation provides a simple example for each of these classes.
Code Development Made Too Easy?
For me, the joy of being a computer scientist is spending long hours writing code for various algorithms. The problem solving keeps my mind alert, and the computations keep going in my head even in sleep. All the utilities in Java 7 take much of that joy of programming away, but they contribute to the bottom line for the companies supporting Java projects, which is what really matters for Java.
For Further Reading
About the Author
|Raghu Donepudi is an independent contractor working as a Technical Manager for a U.S. Federal Government agency. He holds a master’s degree in Computer Science from Lamar University in Beaumont, TX. Raghu is also a Sun-certified Java developer and the author of innovative software design techniques.|