ebook img

Java 11 Cookbook - A Definitive Guide to Learning the Key Concepts of Modern Application Development PDF

791 Pages·2019·14.946 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java 11 Cookbook - A Definitive Guide to Learning the Key Concepts of Modern Application Development

Java 11 Cookbook Second Edition A definitive guide to learning the key concepts of modern application development Nick Samoylov Mohamed Sanaulla BIRMINGHAM - MUMBAI Java 11 Cookbook Second Edition Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Richa Tripathi Acquisition Editor: Denim Pinto Content Development Editor: Pooja Parvatkar Technical Editor: Mehul Singh Copy Editor: Safis Editing Project Coordinator: Ulhas Kambali Proofreader: Safis Editing Indexer: Rekha Nair Graphics: Tom Scaria Production Coordinator: Arvindkumar Gupta First published: August 2017 Second edition: September 2018 Production reference: 1210918 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78913-235-9 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the authors Nick Samoylov graduated as an engineer-physicist from Moscow Institute of Physics and Technology, has worked as a theoretical physicist, and learned how to program as a tool for testing his mathematical models using FORTRAN and C++. After the demise of the USSR, Nick created and successfully ran a software company, but was forced to close it under pressure from governmental and criminal rackets. In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado since then, working as a Java programmer. In his free time, Nick likes to read (mostly non-fiction), write (fiction novels and blogs), and hike the Rocky Mountains. Mohamed Sanaulla is a full-stack developer with more than 8 years, experience in developing enterprise applications and Java-based backend solutions for e-commerce applications. His interests include enterprise software development, refactoring and redesigning applications, designing and implementing RESTful web services, troubleshooting Java applications for performance issues, and TDD. He has strong expertise in Java-based application development, ADF (a JSF-based Java EE web framework), SQL, PL/SQL, JUnit, designing RESTful services, Spring, Spring Boot, Struts, Elasticsearch, and MongoDB. He is also a Sun Certified Java Programmer for the Java 6 platform. He is a moderator for JavaRanch and likes to share his findings on his blog. I would like to thank my family for their encouragement, support, and for putting up with my absence while I was busy compiling this book. I would also like to thank the Packt Publishing team for giving me the opportunity to author the book. About the reviewer Aristides Villarreal Bravo is a Java developer, a member of the NetBeans Dream Team, and a Java User Groups leader. He lives in Panama. He has organized and participated in various conferences and seminars related to Java, JavaEE, NetBeans, the NetBeans platform, free software, and mobile devices. He is the author of jmoordb and tutorials and blogs about Java, NetBeans, and web development. Aristides has participated in several interviews on sites about topics such as NetBeans, NetBeans DZone, and JavaHispano. He is a plugin developer for NetBeans. I would like to thank my mother, father, and all my family and friends. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Installation and a Sneak Peek into Java 11 7 Introduction 7 Installing JDK 18.9 on Windows and setting up the PATH variable 8 How to do it... 8 Installing JDK 18.9 on Linux (Ubuntu, x64) and configuring the PATH variable 12 How to do it... 12 Compiling and running a Java application 13 Getting ready 14 How to do it... 14 What's new in Java 11? 16 Getting ready 16 How to do it... 17 JEP 318 – Epsilon 17 JEP 321 – HTTP Client (Standard) 18 JEP 323 – Local-Variable Syntax for Lambda Parameters 18 JEP 333 – ZGC 18 New API 18 There's more... 21 Using application class-data sharing 21 Getting ready 22 How to do it... 22 Chapter 2: Fast Track to OOP - Classes and Interfaces 25 Introduction 25 Implementing object-oriented design (OOD) 26 Getting ready 27 How to do it... 27 How it works... 29 There's more... 30 Using inner classes 31 Getting ready 32 How to do it... 32 How it works... 34 There's more... 37 Using inheritance and aggregation 37 Getting ready 38 Table of Contents How to do it... 38 How it works... 39 Aggregation makes the design more extensible 46 Coding to an interface 48 Getting ready 49 How to do it... 49 How it works... 51 There's more... 53 Creating interfaces with default and static methods 54 Getting ready 55 How to do it... 56 How it works... 58 Creating interfaces with private methods 59 Getting ready 59 How to do it... 59 How it works... 60 There's more... 61 A better way to work with nulls using Optional 62 Getting ready 62 How to do it... 63 How it works... 68 There's more... 69 Using the utility class Objects 70 Getting ready 70 How to do it... 71 How it works... 78 Chapter 3: Modular Programming 80 Introduction 80 Using jdeps to find dependencies in a Java application 82 Getting ready 82 How to do it... 84 How it works... 90 There's more... 91 Creating a simple modular application 92 Getting ready 92 How to do it... 92 How it works... 99 See also 101 Creating a modular JAR 101 Getting ready 101 How to do it... 102 Using a module JAR with pre-Project Jigsaw JDK applications 103 Getting ready 104 How to do it... 104 [ ii ] Table of Contents Bottom-up migration 106 Getting ready 107 How to do it... 109 Modularizing banking.util.jar 110 Modularizing math.util.jar 112 Modularizing calculator.jar 115 How it works... 117 Top-down migration 117 Getting ready 118 How to do it... 119 Modularizing the calculator 119 Modularizing banking.util 121 Modularizing math.util 122 Using services to create loose coupling between the consumer and provider modules 124 Getting ready 124 How to do it... 124 Creating a custom modular runtime image using jlink 129 Getting ready 130 How to do it... 131 Compiling for older platform versions 132 Getting ready 132 How to do it... 133 How it works... 135 Creating multi-release JARs 135 How to do it... 135 How it works... 137 Using Maven to develop a modular application 138 Getting ready 138 How to do it... 139 Making your library module-path-friendly 142 Getting ready 143 How to do it... 143 How it works... 146 There's more... 147 How to open a module for reflection 148 Getting ready 148 How to do it... 149 How it works... 150 Chapter 4: Going Functional 151 Introduction 151 Using standard functional interfaces 152 Getting ready 153 How to do it... 155 How it works... 159 [ iii ] Table of Contents There's more... 162 Creating a functional interface 163 Getting ready 164 How to do it... 165 How it works... 167 There's more... 167 Understanding lambda expressions 169 Getting ready 169 How to do it... 170 How it works... 171 There's more... 172 Using lambda expressions 173 Getting ready 173 How to do it... 176 How it works... 179 There's more... 181 Using method references 181 Getting ready 181 How to do it... 181 Static unbound method reference 182 Non-static bound method reference 183 Non-static unbound method reference 186 Constructor method references 187 There's more... 190 Leveraging lambda expressions in your programs 191 Getting ready 191 How to do it... 194 There's more... 202 Chapter 5: Streams and Pipelines 204 Introduction 205 Creating immutable collections using the of() and copyOf() factory methods 205 Getting ready 206 How to do it... 209 There's more... 211 Creating and operating on streams 212 Getting ready 212 How to do it... 214 How it works... 220 Using numeric streams for arithmetic operations 232 Getting ready 233 How to do it... 234 There's more... 239 Completing streams by producing collections 241 [ iv ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.