Java 8 New Features: Practical Heads-Up Guide [Smashwords Edition] Richard Reese
* * * * * Published in 2014 by P8tech, an imprint of Play Technologies (England) Limited Copyright Play Technologies (England) Limited Richard Reese asserted their rights under the Copyright, Designs and Patents Act, 1988 to be identified as the author of this book. ISBN: 978-0-9929105-4-9 All Rights Reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the publisher. This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, re-sold, hired out or otherwise circulated without the publishers prior consent in any form of binding or cover other than that it which it is published and without a similar condition including this condition being imposed on the subsequent purchaser. P8tech has endeavoured to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, P8tech cannot guarantee the accuracy of this information.
Published by P8tech Limited
6 Woodside
Churnet View Road
Oakamoor
ST10 3AE
United Kingdom www.p8tech.com Cover image Shutterstock/Maks Narodenko * * * * * Table of Contents About The Author Richard Reese is an acclaimed author who has written several Java and C books, bringing a focused and easy-to-follow approach to learning. He currently teaches at Tarleton State University. Richard is currently working on a JMonkeyEngine title. * * * * * Code Examples Read Me You are reading an electronic version of this book. Some eReaders (and different screen widths) can do some quite funky reformatting to text, including stripping out indentations. To get your hands on the code as .txt, it is available as a freely downloadable file. To get your hands on the code as .txt, it is available as a freely downloadable file.
Download the file from the publisher page at: www.P8tech.com/java701 * * * * * Errata Despite best efforts, mistakes can sometimes creep into books. If you spot a mistake, please feel free to email us at errata@p8tech.com (with the book title in the subject line). The errata page for the book is hosted at www.P8tech.com/java702 * * * * * Chapter 1 Java 8 and Interface Enhancements Introduction to Java 8 -Default Methods and Functional Interfaces -Creating Default Methods -Understanding the Importance of Default Methods -Defining a Default Method --Overriding Default Methods -Using Inheritance with Default Methods --Working with Single Inheritance --Working with Multiple Inheritance --Understanding how Diamond Inheritance Works -Resolving Overridden Default Methods -Using Default Methods --Extending Existing Interfaces --Using Default Methods to Supplement Adapter Classes --Using Default Methods in Core Java Classes --Using a Class to Support Default Methods -Understanding the Difference between an Abstract Class and Interfaces -Using Static Interface Methods -Functional Interfaces --Creating a Functional Interface --Using the @FunctionalInterface Annotation --Overriding Object Class Methods in a Functional Interface --Using Functional Interfaces in the Core Libraries -Conclusion Introduction to Java 8 For Java to remain competitive it has to evolve. The enhancements to version 8 of Java introduce a number of new language features and packages. In this book we will examine most of these additions starting with an explanation of default methods and function interfaces. These new features, whilst useful in themselves, support other Java 8 features such as lambda expressions and stream.
Interfaces are the focus of the first chapter. One of the goals for Java 8 was to support and encourage the use of a functional style of programming. While this has not been completely achieved, the addition of lambda expressions certainly encourages this style. Lambda expressions are essentially anonymous functions. With Java 8 come a number of new function-like libraries designed to work with lambda expressions. Chapter 2 explores how to create and use these expressions.
Another goal of Java 8 is to provide better support for concurrent processing. Earlier techniques, while workable, can be error prone and hard to implement. The introduction of Streams to Java 8 moves some of the conceptual difficulties of concurrent programming away from the programmer and embeds them in streams. A stream can be thought of as a collection of elements to be processed either sequentially or concurrently. The programmer can specify what needs to be done and not worry about how it is done. In addition, there is support for internal iteration within the collection framework with the addition of the forEach method.
We will see many applications of streams in Chapter 3. Handling date and time has never been as clean as it could be in Java. This is rectified in Java 8 with the introduction of the java.time package and new classes to support time related tasks. The improvements introduced help make the execution of these tasks more readable and reliable. The formatting of time has also been improved. These enhancements are presented in Chapter 4.
There have been numerous other additions to Java. These will be discussed in Chapter 5. They range from virtual machine enhancements, which are not always readily visible to the typical programmer, to enhancements in how Java applications are annotated and documented. In addition, there are a few concurrency and character encoding updates. Also, the Nashorn JavaScript engine has been incorporated into Java which enables execution of JavaScript code from within a Java application. Default Methods and Functional Interfaces This chapter focuses on the various changes that have been made to interfaces and how they can be defined and used.
It starts out with a discussion of default methods. These types of methods are an important aspect of Java 8 as they lay the groundwork for other enhancements such as lambda expressions and streams. We cover how default expressions are created and used both in old applications and in new ones. Default methods are methods of an interface that have implementations. This means that classes that implement these types of interfaces do not have to implement default methods. Default methods have been added to existing interfaces such as those found in many collection classes.
Since classes that implement such interfaces do not need to override those methods, existing code that uses these interfaces will still work properly. As with earlier versions of Java, inheritance between interfaces is supported. However, default methods add a new dimension to inheritance and offer opportunities for overriding methods in both single and multiple inheritance situations. Functional interfaces are interfaces that possess one, and only one, abstract method. These types of interfaces are also called Single Abstract Methods (SAM). While an interface can possess default methods, it can have only one abstract method.
As demonstrated in Chapter 2, this type of interface provides the foundation for lambda expressions. Creating Default Methods Default methods are interface methods that have an implementation. They are declared using the default keyword. An example of a default method follows with a Drawable interface definition consisting of one default method and one abstract method: interface Drawable { public default void draw() { System.out.println( "The Drawable interface's draw method"); } public abstract boolean hasBeenDrawn(); }