Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

Preface

The super-fast evolution of the JDK between versions 12 and 21 means two things: first, Java now has a host of powerful new features that you can adopt to solve a variety of modern-day problems; second, the learning curve of modern Java is becoming very steep.

This book enables you to take an objective approach to solving common problems by explaining the correct practices and decisions you need to make with respect to complexity, performance, readability, and more.

Java Coding Problems, Second Edition, will help you complete your daily tasks and meet deadlines, all while becoming a more proficient and self-sufficient Java developer. You can count on the 270+ (all brand-new for this edition) problems in this book to cover the most common and fundamental areas of interest: strings, numbers, arrays, collections, the Foreign Function and Memory API, data structures, date and time, pattern matching, sealed/hidden classes, functional programming, virtual threads, Structured Concurrency, Garbage Collectors, Dynamic CDS Archives, the Socket API, and Simple Web Server.

Put your skills on steroids with problems that have been carefully crafted to highlight and teach the core knowledge needed for daily work. In other words (no matter if your task is simple, middling, or complex), having this knowledge in your tool belt is a must, not an option.

By the end of this book, you will have gained a strong understanding of Java concepts, and you’ll have the confidence to develop and choose the right solutions to all your Java problems.

While this book is entirely stand-alone, and you do not need anything else to get the most out of it, many of the topics covered in this book are also explored in Java Coding Problems, First Edition. If you haven’t already read it, and you wish to get even more practice in, then consider picking that book up for a completely different set of Java problems.

Who this book is for

Java Coding Problems, Second Edition, is especially useful for late-beginner-to-intermediate Java developers looking to level-up their knowledge by solving real-world problems. However, the problems looked at within these pages will be encountered in the daily work of any Java developer, from beginner all the way to advanced practitioner.

Therefore, it is recommended that you are familiar with the Java fundamentals and have at least a foundational working knowledge of the language.

What this book covers

Chapter 1, Text Blocks, Locales, Numbers, and Math, includes 37 problems covering 4 main topics, text blocks, locales, numbers, and mathematical operations.

Chapter 2, Objects, Immutability, Switch Expressions, and Pattern Matching, includes 30 problems tackling, among other things, some less-known features of java.util.Objects, some interesting aspects of immutability, the newest features of switch expressions, and deep coverage of the pattern matching expressions; instanceof and switch.

Chapter 3, Working with Date and Time, includes 20 problems covering different date-time topics. These problems are mainly focused on the Calendar API and the JDK 8 Date/Time API. About the latter, we will cover some of the less-studied APIs like ChronoUnit, ChronoField, IsoFields, and TemporalAdjusters.

Chapter 4, Records and Record Patterns, includes 19 problems that cover in detail the Java records introduced in JDK 16 (JEP 395) and record patterns, which were introduced as a preview feature in JDK 19 (JEP 405), as a second preview feature in JDK 20 (JEP 432), and as a final feature in JDK 21 (JEP 440).

Chapter 5, Arrays, Collections, and Data Structures, includes 24 problems covering three main topics. We start with several problems meant to cover the new Vector API dedicated to data parallel processing. We continue with several data structures including Rope, Skip List, K-D Tree, Zipper, Binomial Heap, Fibonacci Heap, Pairing Heap, Huffman Coding, and so on. Finally, we discuss the three most popular join algorithms.

Chapter 6, Java I/O: Context-Specific Deserialization Filters, includes 13 problems related to Java serialization/deserialization processes. We start with classical problems like serializing/deserializing objects to byte[], String, and XML formats. We then continue with JDK 9 deserialization filters meant to prevent deserialization vulnerabilities, and we finish with JDK 17 context-specific deserialization filters.

Chapter 7, Foreign (Function) Memory API, includes 28 problems covering the Foreign Function Memory API and Foreign Linker API. We start with the classical approaches for calling foreign functions, relying on the JNI API and the open-source JNA/JNR libraries. Next, we introduce the new approach delivered under the code name Project Panama. We dissect the most relevant APIs such as Arena, MemorySegment, MemoryLayout, and so on. Finally, we focus on the Foreign Linker API and the Jextract tool for calling foreign functions that have different types of signatures, including callback functions.

Chapter 8, Sealed and Hidden Classes, includes 13 problems covering Sealed and Hidden Classes. The first 11 recipes will cover Sealed Classes, a very cool feature brought in by JDK 17 to sustain closed hierarchies. The last two problems cover Hidden Classes, a JDK 15 feature that allows frameworks to create and use runtime (dynamic) classes hidden to the JVM.

Chapter 9, Functional Style Programming – Extending APIs, includes 24 problems covering a wide range of functional programming topics. We will start by introducing the JDK 16 mapMulti() and continue with a handful of problems for working with predicates (Predicate), functions, and collectors.

Chapter 10, Concurrency – Virtual Threads and Structured Concurrency, includes 16 problems briefly introducing virtual threads and structured concurrency.

Chapter 11, Concurrency – Virtual Threads and Structured Concurrency: Diving Deeper, includes 18 problems meant to dive deep into how virtual threads and structured concurrency work and how they should be harnessed in your applications.

Chapter 12, Garbage Collectors and Dynamic CDS Archives, includes 15 problems covering Garbage Collectors and Application Class-Data Sharing (AppCDS).

Chapter 13, Socket API and Simple Web Server, includes 11 problems covering the Socket API and 8 problems covering JDK 18’s Simple Web Server. In the first 11 problems, we will discuss implementing socket-based applications such as blocking/non-blocking server/client applications, datagram-based applications, and multicast applications. In the second part of this chapter, we discuss Simple Web Server as a command-line tool.

To get the most out of this book

You should have a fundamental understanding of the Java language. You should also install the following:

  • An IDE (the recommended, but not essential, choice is Apache NetBeans 20.x: https://netbeans.apache.org/front/main/).
  • JDK 21 and the Maven latest release.
  • Additional third-party libraries will need to be installed to fully follow along with some problems and chapters (nothing too difficult or special).

Download the example code files

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Java-Coding-Problems-Second-Edition. In case there’s an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781837633944.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles.

Here is an example: “For instance, let’s add the Patient and Appointment records as well.”

A block of code is set as follows:

if (staff instanceof Resident(String name, Doctor dr)) { 
  return "Cabinet of " + dr.specialty() + ". Doctor: " 
                       + dr.name() + ", Resident: " + name;
}

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

if (staff instanceof Resident(String name, Doctor dr)) { 
  return "Cabinet of " + dr.specialty() + ". Doctor: " 
                       + dr.name() + ", Resident: " + name;
}

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

2023-02-07T05:26:17.374159500Z
2023-02-07T05:26:17.384811300Z
2023-02-07T05:26:17.384811300Z

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: “The compiler recognizes a Java record via the record keyword.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com, and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit, http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read Java Coding Problems, Second Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below
Qr code  Description automatically generated

https://packt.link/free-ebook/9781837633944

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard