Book Image

Java 7 New Features Cookbook

By : Richard M. Reese, Jennifer L. Reese
Book Image

Java 7 New Features Cookbook

By: Richard M. Reese, Jennifer L. Reese

Overview of this book

<p>Java 7 is a major update that includes a lot of exciting new language improvements such as support for type inference and improved exception handling. Other new features include the ability to work with symbolic links, a greatly simplified directory traversal technique, and the monitoring of file creation and deletion. Improvements in event handling, security, and concurrent processing have also been added<br /><br />Java 7 New Features Cookbook is your go-to guide to learn about all the new exciting features Java 7 has to offer with a very practical recipe-based approach. <br /><br />The book starts with coverage of the new language improvements. Subsequent chapters address the new features of Java 7 while incorporating these new language improvements when possible.<br /><br />The new NIO techniques provide enhanced capabilities which are complemented by the new try-with-resources block and enhanced generic support. The new JLayer decorator and improved window methods enhance the developer&rsquo;s ability to create GUI applications. <br /><br />The Java 7 New Features Cookbook provides a comprehensive coverage of the exciting features in Java 7.</p>
Table of Contents (18 chapters)
Java 7 New Features Cookbook
Credits
About the Authors
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface

Preface

With the release of Java 7, numerous new features have been added that significantly improve the developer's ability to create and maintain Java applications. These include language improvements, such as better exception handling techniques, and additions to the Java core libraries, such as new threading mechanisms.

This cookbook covers these new features using a series of recipes. Each recipe addresses one or more new features and provides a template for using these features. This should make it easier to understand the features along with when and how they can be used. Step-by-step instructions are provided to guide the reader through the recipes and are followed by an explanation of the resulting code.

The book starts with a discussion of the new language enhancements, which is followed by a series of chapters, each addressing a specific area such as file and directory management. The reader is assumed to be familiar with the features of Java 6. The book does not need to be read in sequential order, which enables the reader to choose the chapters and recipes that are of interest. However, it is recommended that the reader cover the first chapter, as many of the features found there will be used in subsequent recipes. If other new Java 7 features are used in a recipe, then cross references are provided to the related recipes.

What this book covers

Chapter 1, Java Language Improvements: In this chapter, we examine the various language improvements introduced as part of Project Coin. These features include simple improvements such as using underscores in literals and the use of strings with switch statements. Also, more significant improvements such as the try-with-resources block and the introduction of the diamond operator are detailed.

Chapter 2, Locating Files and Directories Using Paths: The Path class is introduced in this chapter. It is used in this and other chapters and is the basis for much of the new file-related additions to Java 7.

Chapter 3, Obtaining File and Directory Information: Many applications need access to specific file and directory information. How to access this file information is addressed here, including accessing such information as the basic file attributes, Posix attributes, and a file's access control list.

Chapter 4, Managing Files and Directories: In this chapter, the basic mechanisms for managing files and directories are covered, including such actions as creating and deleting files. Also addressed are the use of temporary files and the management of symbolic links.

Chapter 5, Managing File Systems: Here a number of interesting topics, such as how to obtain the filesystem and file store information, the classes used to traverse a file structure, how to watch for file and directory events, and how to work with a ZIP file system are presented.

Chapter 6, Stream IO in Java 7: NIO2 is introduced. New techniques for performing asynchronous IO are detailed along with new approaches for performing random access IO and using a secure directory stream.

Chapter 7, Graphical User Interface Improvements: There have been several additions to Java 7 to address the creation of a GUI interface. It is now possible to create windows with different shapes and windows that are transparent. In addition, numerous enhancements are explained such as the use of the JLayer decorator, which improves the ability to overlay graphics on a window.

Chapter 8, Handling Events: In this chapter, new methods for working with various application events are examined. Java 7 now supports extra mouse buttons and precision mouse wheels. The ability to control a window's focus has been improved and secondary loops have been introduced to mimic the behavior of modal dialog boxes.

Chapter 9, Database, Security, and System Enhancements: Various database improvements such as the introduction of the new RowSetFactory class are illustrated along with how to take advantage of new SSL support. In addition, other system improvements such as additional support for MXBeans are demonstrated.

Chapter 10, Concurrent Processing: Several new classes have been added to support the use of threads, including classes that support the fork/join paradigm, the phaser model, an improved dequeue class, and a transfer queue class. The new ThreadLocalRandom class, used to generate random numbers, is explained.

Chapter 11, Odds and Ends: This chapter demonstrates many other Java 7 improvements such as new support for week, years, and currency. Also included in this chapter is the improved support for dealing with null references.

What you need for this book

The software required for this book includes the Java Development Kit (JDK) 1.7 or later. Any integrated development environment that supports Java 7 can be used to create and execute the recipes. The examples in this book were developed using NetBeans 7.0.1.

Who this book is for

This book is designed to bring those who are familiar with Java up-to-speed on the new features found in Java 7.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

private void gameEngine(List<Entity> entities)
{
final Phaser phaser = new Phaser(1);
for (final Entity entity : entities)
{
final String member = entity.toString();
System.out.println(member + " joined the game");
phaser.register();
new Thread()
{
@Override
public void run()
{
System.out.println(member +
" waiting for the remaining participants");
phaser.arriveAndAwaitAdvance(); // wait for remaining entities
System.out.println(member + " starting run");
entity.run();
}
}.start();
}
phaser.arriveAndDeregister(); //Deregister and continue
System.out.println("Phaser continuing");
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

private void gameEngine(List<Entity> entities)
{
final Phaser phaser = new Phaser(1);
for (final Entity entity : entities)
{
final String member = entity.toString();
System.out.println(member + " joined the game");
phaser.register();
new Thread()
{
@Override
public void run()
{
System.out.println(member +
" waiting for the remaining participants");
phaser.arriveAndAwaitAdvance(); // wait for remaining entities
System.out.println(member + " starting run");
entity.run();
}
}.start();
}
phaser.arriveAndDeregister(); //Deregister and continue
System.out.println("Phaser continuing");
}

Any command-line input or output is written as follows:

Paths.get(new URI("file:///C:/home/docs/users.txt")), Charset.defaultCharset()))

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.