Java 17 Recipes - A problem-solution approach

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Introduction ------------ Quickly find solutions to dozens of common programming problems encountered while building Java applications, with recipes presented in the popular problem-solution format. Look up the programming problem that you want to resolve. Read the solution. Apply the solution directly in your own code. Problem solved! Java 17 Recipes is updated to reflect changes in specification and implementation since the Java 9 edition of this book. Java 17 is the next long-term support release (LTS) of the core Java Standard Edition (SE) version 17 which also includes some of the features from previous short term support (STS) releases of Java 16 and previous versions. This new edition covers of some of the newest features, APIs, and more such as pattern matching for switch, Restore Always-Strict-Floating-Point-Semantics, enhanced pseudo-random number generators, the vector API, sealed classes, and enhancements in the use of String. Source code for all recipes is available in a dedicated GitHub repository. This must-have reference belongs in your library. -------------------------- What You Will Learn =================== Look up solutions to everyday problems involving Java SE 17 LTS and other recent releases Develop Java SE applications using the latest in Java SE technology Incorporate Java major features introduced in versions 17, 16, and 15 into your code ================================= About the Author Josh Juneau has been developing software and enterprise applications since the early days of Java EE. Application and database development have been his focus since the start of his career. He became an Oracle database administrator and adopted the PL/SQL language for performing administrative tasks and developing applications for the Oracle database. In an effort to build more complex solutions, he began to incorporate Java into his PL/SQL applications and later developed standalone and web applications with Java. Josh wrote his early Java web applications utilizing JDBC and servlets or JSP to work with backend databases. Later, he began to incorporate frameworks into his enterprise solutions, such as Java EE and JBoss Seam. Today, he primarily develops enterprise web solutions utilizing Java EE and other technologies. He also includes the use of alternative languages, such as Jython and Groovy, for some of his projects. Over the years, Josh has dabbled in many different programming languages, including alternative languages for the JVM, in particular. In 2006, Josh began devoting time to the Jython Project as editor and publisher of the Jython Monthly newsletter. In late 2008, he began a podcast dedicated to the Jython programming language. Josh was the lead author for The Definitive Guide to Jython, Oracle PL/SQL Recipes, and Java 7 Recipes, and a solo author of Java EE 7 Recipes and Introducing Java EE 7, which were all published by Apress. He works as an application developer and system analyst at Fermi National Accelerator Laboratory, and he also writes technical articles for Oracle and OTN. He was a member of the JSR 372 and JSR 378 expert groups, and is an active member of the Java Community, helping to lead the Chicago Java User Group’s Adopt-a-JSR effort. When not coding or writing, Josh enjoys spending time with his wonderful wife and five children, especially swimming, fishing, playing ball, and watching movies. To hear more from Josh, follow him on Twitter at @javajuneau. Luciano Manelli earned a PhD in computer science from the IT department, University of Bari-Aldo Moro. His PhD focused on grid computing and formal methods, and he published the results in international publications. Luciano obtained several certificates in the IT sector, and, in 2014, began working for the Port Network Authority of the Ionian Sea–Port of Taranto, after working for 13 years for InfoCamere SCpA. He has worked mainly in the design, analysis, and development of large software systems; research and development; testing; and production with roles of increasing responsibility in several areas over the years. Luciano has developed a great capability to make decisions in a technical and business context and is mainly interested in project management and business process management. In his current position, he deals with port community systems and software innovation. Additionally, he has written several IT books and is a contract professor at the Polytechnic of Bari (foundations of computer science), and at the University of Bari-Aldo Moro (programming for web, computer science, and computer lab).

Author(s): Josh Juneau
Edition: 4
Publisher: Apress
Year: 2022

Language: English
Tags: java java 17 jdk web microservices

Chapter 1: Getting Started with Java 17
1-1. Installing Java
Problem
Solution
How It Works
1-2. Configuring the PATH
Problem
Solution
How It Works
1-3. Testing Java
Problem
Solution
How It Works
The Main Program
1-4. Installing Eclipse
Problem
Solution
How It Works
1-5. Getting to “Hello, World”
Problem
Solution
How It Works
Packages
JavaBeans-style Classes
The Main Program
1-6. Configuring the CLASSPATH
Problem
Solution
How It Works
1-7. Organizing Code with Packages
Problem
Solution
How It Works
1-8. Declaring Variables and Access Modifiers
Problem
Solution
How It Works
1-9. Converting to and from a String
Problem
Solution
How It Works
1-10. Passing Arguments via Command-Line Execution
Problem
Solution
How It Works
1-11. Accepting Input from the Keyboard
Problem
Solution
How It Works
1-12. Documenting Your Code
Problem
Solution
How It Works
Formatting the Documentation
Executing the Tool
1-13. Reading Environment Variables
Problem
Solution
How It Works
1-14. Summary
Chapter 2: Enhancements from Java 9 Through Java 17
2-1. Introduction to the var Keyword
Problem
Solution
How It Works
2-2. Reading the Contents of Files
Problem
Solution
How It Works
2-3. Writing a Text Block
Problem
Solution
How It Works
2-4. The Enhancement of NullPointerException
Problem
Solution
How It Works
2-5. Pattern Matching for instanceof
Problem
Solution
How It Works
2-6. Using Record
Problem
Solution
How It Works
2-7. Restore Always-Strict Floating-Point Semantics
Problem
Solution
How It Works
2-8. Pseudorandom Number Generators
Problem
Solution
How It Works
2-9. Sealed Classes
Problem
Solution
How It Works
2-10. The Vector API
Problem
Solution
How It Works
2-11. Avoiding Redundancy in Interface Code
Problem
Solution
How It Works
2-12. Easily Retrieving Information on OS Processes
Problem
Solution
How It Works
2-13. Handling try-with-resources Construct
Problem
Solution
How It Works
2-14. Filtering Data Before and After a Condition with Streams
Problem
Solution
How It Works
2-15. Utilizing Factory Methods to Create Immutable Collections
Problem
Solution
How It Works
2-16. Pattern Matching for switch (Preview)
Problem
Solution
How It Works
2-17. Summary
Chapter 3: Strings
3-1. Compact Strings
3-2. Obtaining a Subsection of a String
Problem
Solution
How It Works
3-3. Comparing Strings
Problem
Solution
How It Works
3-4. Trimming Whitespace
Problem
Solution
How It Works
3-5. Discovering Blank Strings
Problem
Solution
How It Works
3-6. Stripping Whitespace
Problem
Solution
How It Works
3-7. Breaking String Lines
Problem
Solution
How It Works
3-8. Repeating Strings
Problem
Solution
How It Works
3-9. Changing the Case of a String
Problem
Solution
How It Works
3-10. Concatenating Strings
Problem
Solution 1
Solution 2
Solution 3
How It Works
3-11. Converting Strings to Numeric Values
Problem
Solution 1
Solution 2
How It Works
3-12. Iterating Over the Characters of a String
Problem
Solution
How It Works
3-13. Finding Text Matches
Problem
Solution 1
Solution 2
How It Works
3-14. Replacing All Text Matches
Problem
Solution
How It Works
3-15. Determining Whether a File Suffix Matches a Given String
Problem
Solution
How It Works
3-16. Making a String That Can Contain Dynamic Information
Problem
Solution 1
Solution 2
How It Works
3-17. Summary
Chapter 4: Numbers and Dates
4-1. Rounding Float and Double Values to Integers
Problem
Solution
How It Works
4-2. Formatting Double and Long Decimal Values
Problem
Solution
How It Works
4-3. Formatting Compact Number
Problem
Solution
How It Works
4-4. Comparing int Values
Problem
Solution 1
Solution 2
How It Works
4-5. Comparing Floating-Point Numbers
Problem
Solution 1
Solution 2
How It Works
4-6. Randomly Generating Values
Problem
Solution 1
Solution 2
How It Works
4-7. Obtaining the Current Date Without Time
Problem
Solution
How It Works
4-8. Obtaining a Date Object Given Date Criteria
Problem
Solution
How It Works
4-9. Obtaining a Year-Month-Day Date Combination
Problem
Solution 1
Solution 2
How It Works
4-10. Obtaining and Calculating Times Based on the Current Time
Problem
Solution
How It Works
4-11. Obtaining and Using the Date and Time Together
Problem
Solution 1
Solution 2
Solution 3
How It Works
4-12. Obtaining a Machine Timestamp
Problem
Solution
How It Works
4-13. Converting Dates and Times Based on he Time Zone
Problem
Solution
How It Works
4-14. Comparing Two Dates
Problem
Solution
How It Works
4-15. Finding the Interval Between Dates and Times
Problem
Solution 1
Solution 2
How It Works
4-16. Obtaining Date-Time from a Specified String
Problem
Solution
How It Works
4-17. Formatting Dates for Display
Problem
Solution 1
Solution 2
How It Works
4-18. Writing Readable Numeric Literals
Problem
Solution
How It Works
4-19. Declaring Binary Literals
Problem
Solution
How It Works
4-20. Period of Day
Problem
Solution
How It Works
4-21. Summary
Chapter 5: Object-Oriented Java
5-1. Controlling Access to Members of a Class
Problem
Solution
How It Works
5-2. Making Private Fields Accessible to Other Classes
Problem
Solution
How It Works
5-3. Creating a Class with a Single Instance
Problem
Solution 1
Solution 2
How It Works
5-4. Generating Instances of a Class
Problem
Solution
How It Works
5-5. Creating Reusable Objects
Problem
Solution
How It Works
5-6. Defining an Interface for a Class
Problem
Solution
How It Works
5-7. Modifying Interfaces Without Breaking Existing Code
Problem
Solution
How It Works
5-8. Constructing Instances of the Same Class with Different Values
Problem
Solution
How It Works
5-9. Interacting with a Class via Interfaces
Problem
Solution
How It Works
5-10. Making a Class Cloneable
Problem
Solution
How It Works
5-11. Comparing Objects
Problem
Solution 1
Solution 2
How It Works
5-12. Extending the Functionality of a Class
Problem
Solution
How It Works
5-13. Defining a Template for Classes to Extend
Problem
Solution
How It Works
5-14. Increasing Class Encapsulation
Problem
Solution
How It Works
5-15. Summary
Chapter 6: Lambda Expressions
6-1. Writing a Simple Lambda Expression
Problem
Solution
How It Works
6-2. Enabling the Use of Lambda Expressions
Problem
Solution 1
Solution 2
How It Works
6-3. Invoking Existing Methods by Name
Problem
Solution
How It Works
6-4. Sorting with Fewer Lines of Code
Problem
Solution 1
Solution 2
How It Works
6-5. Filtering a Collection of Data
Problem
Solution
How It Works
6-6. Implementing Runnable
Problem
Solution
How It Works
6-7. Accessing Class Variables from a Lambda Expression
Problem
Solution
How It Works
6-8. Passing Lambda Expressions to Methods
Problem
Solution
How It Works
6-9. Local Variable
Problem
Solution
How It Works
6-10. Switch Expressions
Problem
Solution
How It Works
6-11. Summary
Chapter 7: Data Sources and Collections
7-1. Defining a Fixed Set of Related Constants
Problem
Solution
How It Works
7-2. Designing Intelligent Constants
Problem
Solution
How It Works
7-3. Executing Code Based on a Specified Value
Problem
Solution
How It Works
7-4. Working with Fix-Sized Arrays
Problem
Solution
How It Works
7-5. Safely Enabling Types or Methods to Operate on Objects of Various Types
Problem
Solution
How It Works
7-6. Working with Dynamic Arrays
Problem
Solution
How It Works
7-7. Making Your Objects Iterable
Problem
Solution
How It Works
7-8. Iterating Collections
Problem
Solution
How It Works
7-9. Iterating Over a Map
Problem
Solution
How It Works
7-10. Executing Streams in Parallel
Problem
Solution
How It Works
7-11. Summary
Chapter 8: Input and Output
8-1. Serializing Java Objects
Problem
Solution
How It Works
8-2. Serializing Java Objects More Efficiently
Problem
Solution
How It Works
8-3. Serializing Java Objects as XML
Problem
Solution
How It Works
8-4. Creating a Socket Connection and Sending Serializable Objects Across the Wire
Problem
Solution
How It Works
8-5. Obtaining the Java Execution Path
Problem
Solution
How It Works
8-6. Copying a File
Problem
Solution
How It Works
8-7. Moving a File
Problem
Solution
How It Works
8-8. Iterating Over Files in a Directory
Problem
Solution
How It Works
8-9. Querying (and Setting) File Metadata
Problem
Solution
How It Works
8-10. Monitoring a Directory for Content Changes
Problem
Solution
How It Works
8-11. Reading Property Files
Problem
Solution
How It Works
8-12. Uncompressing Files
Problem
Solution
How It Works
8-13. Summary
Chapter 9: Exceptions and Logging
9-1. Catching Exceptions
Problem
Solution
How It Works
9-2. Guaranteeing a Block of Code Is Executed
Problem
Solution
How It Works
9-3. Throwing Exceptions
Problem
Solution
How It Works
9-4. Catching Multiple Exceptions
Problem
Solution 1
Solution 2
How It Works
9-5. Catching the Uncaught Exceptions
Problem
Solution 1
Solution 2
How It Works
9-6. Managing Resources with try/catch Blocks
Problem
Solution
How It Works
9-7. Creating an Exception Class
Problem
Solution 1
Solution 2
How It Works
9-8. Logging Events Within Your Application
Problem
Solution
How It Works
9-9. Rotating and Purging Logs
Problem
Solution
How It Works
9-10. Logging Exceptions
Problem
Solution
How It Works
9-11. Summary
Chapter 10: Concurrency
10-1. Starting a Background Task
Problem
Solution
How It Works
10-2. Updating (and Iterating) a Map
Problem
Solution
How It Works
10-3. Inserting a Key into a Map Only If the Key Is Not Already Present
Problem
Solution
How It Works
10-4. Iterating Through a Changing Collection
Problem
Solution 1
Solution 2
How It Works
10-5. Coordinating Different Collections
Problem
Solution 1
Solution 2
How It Works
10-6. Splitting Work into Separate Threads
Problem
Solution
How It Works
10-7. Coordinating Threads
Problem
Solution 1
Solution 2
Solution 3
How It Works
10-8. Creating Thread-Safe Objects
Problem
Solution 1
Solution 2
How It Works
10-9. Implementing Thread-Safe Counters
Problem
Solution
How It Works
10-10. Updating a Common Value Across Multiple Threads
Problem
Solution
How It Works
10-11. Executing Multiple Tasks Asynchronously
Problem
Solution
How It Works
10-12. Summary
Chapter 11: Unicode, Internationalization, and Currency Codes
11-1. Converting Unicode Characters to Digits
Problem
Solution
How It Works
11-2. Creating and Working with Locales
Problem
Solution
How It Works
11-3. Matching and Filtering Locales
Problem
Solution
How It Works
11-4. Searching Unicode with Regular Expressions
Problem
Solution 1
Solution 2
How It Works
11-5. Overriding the Default Currency
Problem
Solution
How It Works
11-6. Converting Byte Arrays to and from Strings
Problem
Solution
How It Works
11-7. Converting Character Streams and Buffers
Problem
Solution 1
Solution 2
How It Works
11-8. Summary
Chapter 12: Working with Databases
12-1. Installing MySQL
Problem
Solution
How It Works
12-2. Connecting to a Database
Problem
Solution
How It Works
12-3. Handling Connection and SQL Exceptions
Problem
Solution
How It Works
12-4. Querying a Database and Retrieving Results
Problem
Solution
How It Works
12-5. Performing CRUD Operations
Problem
Solution
How It Works
12-6. Simplifying Connection Management
Problem
Solution
How It Works
12-7. Guarding Against SQL Injection
Problem
Solution
How It Works
12-8. Performing Transactions
Problem
Solution
How It Works
12-9. Creating a Scrollable ResultSet
Problem
Solution
How It Works
12-10. Creating an Updatable ResultSet
Problem
Solution
How It Works
12-11. Caching Data for Use When Disconnected
Problem
Solution
How It Works
12-12. Obtaining Dates for Database Use
Problem
Solution
How It Works
12-13. Closing Resources Automatically
Problem
Solution
How It Works
12-14. Summary
Chapter 13: Java Web Applications
13-1. Installing Tomcat
Problem
Solution
How It Works
13-2. Creating an HTML Page
Problem
Solution
How It Works
13-3. Creating a JSP Page
Problem
Solution
How It Works
13-4. Listing the HTML-Request Parameters
Problem
Solution
How It Works
13-5. Creating and Configuring a Web Project
Problem
Solution
How It Works
13-6. Creating a Servlet
Problem
Solution
How It Works
13-7. Using a Servlet for Representing Values
Problem
Solution
How It Works
13-8. Summary
Chapter 14: Email
14-1. Installing JavaMail
Problem
Solution
How It Works
14-2. Sending an Email
Problem
Solution
How It Works
14-3. Attaching Files to an Email Message
Problem
Solution
How It Works
14-4. Sending an HTML Email
Problem
Solution
How It Works
14-5. Sending Email to a Group of Recipients
Problem
Solution
How It Works
14-6. Checking Email
Problem
Solution
How It Works
14-7. Summary
Chapter 15: JSON and XML Processing
15-1. Writing an XML File
Problem
Solution
How It Works
15-2. Reading an XML File
Problem
Solution 1
Solution 2
How It Works
15-3. Transforming XML
Problem
Solution
How It Works
15-4. Validating XML
Problem
Solution
How It Works
15-5. Working with JSON
Problem
Solution
How It Works
15-6. Building a JSON Object
Problem
Solution
How It Works
15-7. Writing a JSON Object to File
Problem
Solution
How It Works
15-8. Parsing a JSON Object
Problem
Solution
How It Works
15-9. Summary
Chapter 16: Networking
16-1. Listening for Connections on the Server
Problem
Solution
How It Works
16-2. Defining a Network Connection to a Server
Problem
Solution
How It Works
16-3. Broadcasting to a Group of Recipients
Problem
Solution
How It Works
16-4. Generating and Reading from URLs
Problem
Solution
How It Works
16-5. Parsing a URL
Problem
Solution
How It Works
16-6. Summary